Nordar
Nordar

Reputation: 187

Proper way of constructing a multimap

I have a problem which I've solved, but I'm looking for a better solution.

I have a big list of (let's assume 500 000) entities

class Entity {
   String key1;
   String key2;
   String value1;
   String value2;
}

which I want to map into a multimap Map<String, Map<String, List<Entity>>> based on key1 and key2 the way that the top level map has entity.key1 as key, and the inner map - entity.key2.

Therefore I've constructed a mapper class to do such thing

class EntityMapper {
   Map<String, Map<String, List<Entity>>> mapByKey1AndKey2(List<Entity> entities) {
        Map<String, Map<String, List<Entity>>> accountMap = new HashMap<>();
        entities.forEach(
            entity -> key1Map.merge(entity.key1, newKey2Map(entity), this::mergeKey2Maps)
        );
        return key1Map;
    }

   Map<String, List<Entity>> newKey2Map(Entity entity) {
        Map<String, List<Entity>> key2Map = new HashMap<>();
        key2Map.put(entity.key2, new ArrayList<>(singletonList(entity)));
        return key2Map;
    }

   Map<String, List<Delta>> mergeKey2Maps(Map<String, List<Entity>> oldMap, Map<String, List<Entity>> newMap) {
        for (String key2 : newMap.keySet()) {
            oldMap.merge(key2, newMap.get(key2), this::mergeLists);
        }
        return oldMap;
    }

   List<Entity> mergeLists(List<Entity> oldList, List<Entity> newList) {
        oldList.addAll(newList);
        return oldList;
    }
}

Are there any disadvantages of this approach? Or is there any way to optimize it?

Disclaimer: I'm aware of the existence of composite key mapping, but it won't serve my purpose.

Upvotes: 0

Views: 60

Answers (1)

marstran
marstran

Reputation: 28046

I think this should work:

Map<String, Map<String, List<Entity>> groupedEntities = 
    entities.stream()
            .collect(Collectors.groupingBy(e -> e.key1,
                     Collectors.groupingBy(e -> e.key2)));

I first group the list by key1, then I group the values of the resulting Map by key2.

Upvotes: 1

Related Questions