MultiKey break returned by MapIterator in separate keys

I use Apache Commons compilations to create a MultiKeyMap that will store two keys with one corresponding value, and then use MapIterator to move around the map. The problem is that I need to split the keys returned by MapIterator into separate keys, and not into one “composite” key. Although I could split the string containing the composite key or use reflections, none of these options look very elegant.

To model the problem, I created the following code example

MultiKeyMap multiKeyMap = new MultiKeyMap();

multiKeyMap.put("Key 1A","Key 1B","Value 1");
multiKeyMap.put("Key 2A","Key 2B","Value 2");
multiKeyMap.put("Key 3A","Key 3B","Value 3");

MapIterator it = multiKeyMap.mapIterator();

while (it.hasNext()) {
    it.next();
    System.out.println(it.getKey());
    System.out.println(it.getValue());
}

it.getKey()returns MultiKey [Key 3A, Key 3B], but I want to assign keys to individual variables, for example myKey1 = it.getKey().keys[0]and myKey2 = it.getKey().keys[1], but I can not find anything in the JavaDoc for this.

, MapIterator, , it.getKey()?

+5
1

commons 3.2, MultiKeyMap MultiKey, getKey (int index). getKeys(), Object []. , :

MultiKeyMap multiKeyMap = new MultiKeyMap();

multiKeyMap.put("Key 1A","Key 1B","Value 1");
multiKeyMap.put("Key 2A","Key 2B","Value 2");
multiKeyMap.put("Key 3A","Key 3B","Value 3");

MapIterator it = multiKeyMap.mapIterator();

while (it.hasNext()) {
    it.next();

    MultiKey mk = (MultiKey) it.getKey();

    // Option 1
    System.out.println(mk.getKey(0));
    System.out.println(mk.getKey(1));

    // Option 2
    for (Object subkey : mk.getKeys())
      System.out.println(subkey);

    System.out.println(it.getValue());
}
+8

All Articles