Java Map with Multimap values - how to efficient split the true entries from the false ones

69 Views Asked by At

I have a Map made up of this structure Map<String, Multimap<Boolean, String>> . I want to separate all the true entries in one new map and the false ones in a second. I have the following (below) which works, but seems terribly inefficient. I am hoping someone can provide me a more efficient way to do this. I can use through Java8, but not beyond. Grateful for ideas. Thanks

Map<String, Multimap<Boolean, String>> falseMap = new HashMap<>();
Map<String, Multimap<Boolean, String>> trueMap = new HashMap<>();

map.entrySet().stream().forEach( stringMultimapEntry -> {
    Multimap<Boolean, String> multimapEntryValue = stringMultimapEntry.getValue();
    for(Map.Entry mm : multimapEntryValue.entries()) {
        Multimap<Boolean, String>fm = ArrayListMultimap.create();
        fm.put((Boolean) mm.getKey(), (String) mm.getValue());
        if (mm.getKey().equals(false)) {
           falseMap.put(stringMultimapEntry.getKey(), fm);
        } else {
           trueMap.put(stringMultimapEntry.getKey(), fm);
        }
    }
});

by the way, this uses com.google.common.collect.Multimap map type.

0

There are 0 best solutions below