Reputation: 428
Let's say I have a List<Set<String>>
that looks like this:
[A,B,C,D]
[B,C,D,E]
[C,D,E,F]
[D,E,F,G]
If I want to use each value (A,B,C,D,E,F,G) in each Set<String>
in that List<Set<String>>
and count their occurrence by mapping them, what is a good way for me to implement it? I want to make the output look something like:
A: 1
B: 2
C: 3
D: 4
E: 3
F: 2
G: 1
Upvotes: 3
Views: 141
Reputation: 40062
Just flatten the list into a single stream and use a groupingBy
collector.
groupingBy
would create a list and put collisions (the values associated with duplicate keys) in the list.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]
Upvotes: 4
Reputation: 1694
List<Set<String>> input = new ArrayList<>();
input.add(Set.of("A", "B", "C", "D"));
input.add(Set.of("B", "C", "D", "E"));
input.add(Set.of("C", "D", "E", "F"));
input.add(Set.of("D", "E", "F", "G"));
input.stream()
.flatMap(Collection::stream)
.collect(groupingBy(Function.identity(), counting()))
.entrySet()
.forEach(System.out::println);
importing
import java.util.*;
import java.util.function.Function;
import static java.util.stream.Collectors.counting;
import static java.util.stream.Collectors.groupingBy;
Upvotes: 3
Reputation: 19565
Using Stream API, flatMap
for inner sets should be used to get stream of Strings and after that a frequency map is built:
List<Set<String>> data = Arrays.asList(
Set.of("A", "B", "C", "D"),
Set.of("B", "C", "D", "E"),
Set.of("C", "D", "E", "F"),
Set.of("D", "E", "F", "G")
);
data.stream()
.flatMap(Set::stream)
.collect(Collectors.toMap(s -> s, s -> 1, Integer::sum, LinkedHashMap::new))
.entrySet()
.forEach(System.out::println);
Output:
A=1
B=2
C=3
D=4
E=3
F=2
G=1
Upvotes: 1