5

I'm using the MultiKeyMap from the commons-collections which provide multikey-value pairs. I have 3 keys which are Strings. I have two problems which I don't see how to solve.

How can I iterate over all multikey-value pairs? With a simple HashMap I know it.

Second, how can I get all multikey-value pairs with the first two keys fixed? That means I would like to get something like this multiKey.get("key1","key2",?); Where the third key is not specified.

machinery
  • 5,972
  • 12
  • 67
  • 118
  • 1
    What's wrong with `mapIterator()`? – Marvin Feb 12 '16 at 21:48
  • so, do you have a hashmap of hashmaps? Im curious if you are doing this multi-key design you didnt do something like: multiKey.get("key1:key2"); so that way the string `key` is actually key1 and key2 concatenated together. – Fallenreaper Feb 12 '16 at 21:48
  • @Marvin nailed your first question the and the answer to the second one is that you cannot do partial matching since it's not supported by the API: https://commons.apache.org/proper/commons-collections/apidocs/org/apache/commons/collections4/map/MultiKeyMap.html#decorated() – Nir Alfasi Feb 12 '16 at 21:52
  • @Fallenreaper I have a MultiKeyMap provided by the appache commons library. – machinery Feb 12 '16 at 21:55
  • Ohhh, got ya. Yeah, I have never really used that one before. I wish i could be of some help. :-/ – Fallenreaper Feb 12 '16 at 21:56

3 Answers3

7

Iteration over key-value for MultiKeyMap is similar to hash map:

    MultiKeyMap<String, String> multiKeyMap = new MultiKeyMap();

    multiKeyMap.put( "a1", "b1", "c1", "value1");
    multiKeyMap.put( "a2", "b2", "c2", "value1");

    for(Map.Entry<MultiKey<? extends String>, String> entry: multiKeyMap.entrySet()){
        System.out.println(entry.getKey().getKey(0)
                +" "+entry.getKey().getKey(1)
                +" "+entry.getKey().getKey(2)
                + " value: "+entry.getValue());
    }

For your second request you can write your own method based on the previous iteration.

public static Set<Map.Entry<MultiKey<? extends String>, String>> match2Keys(String first, String second,
                                                                                MultiKeyMap<String, String> multiKeyMap) {
        Set<Map.Entry<MultiKey<? extends String>, String>> set = new HashSet<>();
        for (Map.Entry<MultiKey<? extends String>, String> entry : multiKeyMap.entrySet()) {
            if (first.equals(entry.getKey().getKey(0)) 
                && second.equals(entry.getKey().getKey(1))) {
                set.add(entry);
            }
        }
        return set;
    }
Liviu Stirb
  • 5,876
  • 3
  • 35
  • 40
3

I'm using commons-collections 4.4 version which provides forEach method. It can be used as follows.

MultiKeyMap<String,Integer> multiKeyMap=new MultiKeyMap<>();
        multiKeyMap.put("class 9","Div A",30);
        multiKeyMap.put("class 9","Div B",40);
        multiKeyMap.forEach((key,value)->{
            System.out.println(key.getKey(0)+" & "+key.getKey(1)+" -> "+value);
        });

Output:
class 9 & Div A -> 30
class 9 & Div B -> 40
amdg
  • 2,211
  • 1
  • 14
  • 25
2

You can iterate the list in values():

    for(Object entry: multiKey.values()){ //TODO }

I just figured out this is a four years old question...

FranAguiar
  • 637
  • 3
  • 14
  • 31