Please find programm below :
Map<String, List<String>> map = new HashMap<>();
List<String> l1 = new ArrayList<>();
l1.add("G1");
l1.add("G2");
List<String> l2 = new ArrayList<>();
l2.add("G2");
l2.add("G3");
map.put("R1", l1);
map.put("R2", l2);
Now the current output is :
{R2=[G2, G3], R1=[G1, G2]}
I would like to write logic in Java 8 in such way that, Map keys should become values and values to keys, like below :
{G1=[R1], G2=[R2, R1], G3=[R2]}
I tried with below but it doesn't work :
Map<String, List<String>> mapInversed =
map.entrySet()
.stream()
.collect(Collectors.groupingBy(Map.Entry::getValue, Collectors.mapping(Map.Entry::getKey, Collectors.toList())))
Exception : Type mismatch: cannot convert from Map<Object,List<Object>> to Map<String,List<String>>
But I want it in same type Map<String,List<String>>
anyone could help ?