How Can I Group Values Of List In Java Using Lambda Like We Do In Python
I want to group values of a map based on the key. Let's say  Map map1 = new TreeMap(); map1.put('D', 3); map1.put('B', 2); map1.put('C',  
Solution 1:
If I understand well, you want to group the identical key set of the maps you added in the last map associated with the original key.
importstatic java.util.stream.Collectors.groupingBy;
importstatic java.util.stream.Collectors.mapping;
importstatic java.util.stream.Collectors.toList;
...
Map<Set<String>, List<Integer>> newMap = 
    map.entrySet()
       .stream()
       .collect(groupingBy(e -> e.getValue().keySet(), 
                           mapping(Map.Entry::getKey, toList())));
From the last map, you get the stream of entries (which is a Stream<Entry<Integer, Map<String, Integer>>). There you group the entries by the key set of their map's values. 
Then you map the values of the resulting map using a downstream collector, which collects the keys of the original entries in a List<Integer>.
Output:
{[A, B, C]=[2, 3], [B, C, D]=[1]}
Post a Comment for "How Can I Group Values Of List In Java Using Lambda Like We Do In Python"