I have a hash map
Map<Integer, List<String>> Directmap = new HashMap<Integer, List<String>>() {{
put(0, Arrays.asList(a, b));
put(1, Arrays.asList(b, c));
put(2, Arrays.asList(d));
put(3, Arrays.asList(d, e));
put(4, Arrays.asList(e));
put(5, Arrays.asList());
}};
Directmap: {0=[a, b], 1=[b, c], 2=[d], 3=[d, e], 4=[e], 5=[]}
I want to count the number of keys for each value. For example: "a"
has one key, "b"
has two keys, ..., "e"
has two keys namely 3 and 4.
I tried something like this:
Map<Object, Long> ex = Directmap.entrySet().stream()
.collect(Collectors.groupingBy(e -> e.getKey(), Collectors.counting()));
I want the values with number of keys as the output. Like this :
a=[1], b=[2], c=[1], d=[2], e=[2]