0

Is it possible for me to have multiple keys and multiple values for my .ini file? if so how?

i know we can have single key with multiple values using list or multimaps but can i replicate the same concept for multiple keys like this-keys-a,b,c values-(for row 1)d,e,f and so on for multiple rows what i actually want is this for my ini file

   key1||key2||key3
   value1||value2||value3
   value4||value5||value6

key1 maps to value1 and4 and so on

and all of this should be in one map

4

2 回答 2

1

Re your edit, it looks like you want to retrieve by any of the keys. So all you really have here is a String : List map.

Just loop through that file, split each line on ||, and add the first entry to the first key's list, the second entry to the second key's list, etc.

Very roughly, Java-like pseudocode:

Map<String, List> map = HashMap<String, List>();
String line;
String[] entries;
int index;
String[] keys;

//...open file...

// First line is keys
line = source.readLine();
if (line == null) {
    // File is empty
}
else {
    // Add the keys
    keys = new String[entries.size()];
    index = 0;
    for (String key : entries) {
         keys[index++] = key;
         map.put(key, new LinkedList());
    }

    // Process entries
    while ((line = source.readLine()) != null) {
        entries = line.split("\\|\\|");
        index = 0;
        for (String entry : entries) {
            if (index >= keys.length) {
                break;
            }
            map.get(keys[index++]).add(entry);
        }
    }
}

...or something like that.


Original answer, just for anyone finding this later who wants to go another way:

I can read your question two ways:

If you mean you want to store a List under several keys and use any one of them to retrieve it, you can do that:

map.put(key1, list);
map.put(key2, list);
map.put(key3, list);

// ...later
list = map.get(key1); // or key2, or key3

If you mean you want to retrieve using all of the keys but not the keys individually, then conceptually that's just one key. You could derive from any of the Map or List classes, add your own equals and getHashCode as appropriate to your definition of the "same" key, and use that:

map.put(specialMultiKey, list);

// ...later
list = map.get(specialMultiKey);
于 2013-06-12T06:35:34.127 回答
0

You can use Apache Common Collection's MultiKeyMap: http://commons.apache.org/proper/commons-collections/apidocs/org/apache/commons/collections/map/MultiKeyMap.html

[EDIT: If you don't want to use it, you can write your own implementation of MuliKeyMap by extending Map and providing an implementation similar to what is given in MultiKeyMap.]

于 2013-06-12T06:42:07.430 回答