Just flatten the list into a single stream and use a groupingBy collector.
- first, just stream the list. This create a stream of the smaller lists.
- then you need to stream those. But you don't want 4 streams of letters. You want one stream of 16 letters (or the sum of all the sets). That is what flatMap does. It flattens multiple streams into one.
- then you want to do a frequency count. So you want to group the letters using themselves as a key. By default
groupingBy would create a list and put collisions (the values associated with duplicate keys) in the list.
- but you don't want that, so the
Collectors.counting() says if you see another key that's already there, just keep a count and update the value by 1. So you're counting occurrences of the keys.
List<Set<String>> list = List.of(Set.of("A", "B", "C", "D"),
Set.of("B", "C", "D", "E"),
Set.of("C", "D", "E", "F"),
Set.of("D", "E", "F", "G"));
Map<String, Long> freq =
list.stream().flatMap(Set::stream).collect(Collectors
.groupingBy(a -> a, Collectors.counting()));
freq.entrySet().forEach(System.out::println);
Prints
A=1
B=2
C=3
D=4
E=3
F=2
G=1
Here's a simple example of the default groupingBy behavior. It simply puts the values in a list based on their remainders when dividing by 10. IntStream generates a stream of int primitives so they need to be converted to an object (Integer in this case) to be collected.
Map<Integer, List<Integer>> remainders =
IntStream.range(0, 100).mapToObj(Integer::valueOf)
.collect(Collectors.groupingBy(n -> n % 10));
remainders.entrySet().forEach(System.out::println);
prints
0=[0, 10, 20, 30, 40, 50, 60, 70, 80, 90]
1=[1, 11, 21, 31, 41, 51, 61, 71, 81, 91]
2=[2, 12, 22, 32, 42, 52, 62, 72, 82, 92]
3=[3, 13, 23, 33, 43, 53, 63, 73, 83, 93]
4=[4, 14, 24, 34, 44, 54, 64, 74, 84, 94]
5=[5, 15, 25, 35, 45, 55, 65, 75, 85, 95]
6=[6, 16, 26, 36, 46, 56, 66, 76, 86, 96]
7=[7, 17, 27, 37, 47, 57, 67, 77, 87, 97]
8=[8, 18, 28, 38, 48, 58, 68, 78, 88, 98]
9=[9, 19, 29, 39, 49, 59, 69, 79, 89, 99]