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);