I have a hashmap with some keys pointing to same values. I want to find all the values that are equal and print the corresponding keys.
This is the current code that I
Build a Map
, i.e. a Map
.
Example
Map map = new HashMap<>();
map.put("hello", "0123");
map.put("hola", "0123");
map.put("kosta", "0123");
map.put("da", "03");
map.put("notda", "013");
map.put("twins2", "01");
map.put("twins22", "01");
map.entrySet().stream()
.collect(Collectors.groupingBy(Entry::getValue,
Collectors.mapping(Entry::getKey, Collectors.toList())))
.entrySet().stream()
.filter(e -> e.getValue().size() > 1)
.forEach(System.out::println);
Output
01=[twins22, twins2]
0123=[kosta, hello, hola]
Without the filter()
, the result would be:
01=[twins22, twins2]
013=[notda]
03=[da]
0123=[kosta, hello, hola]