Firefly
Firefly

Reputation: 428

How to count item occurences in a list with sets?

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

Answers (3)

WJS
WJS

Reputation: 40062

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]

Upvotes: 4

Vlad L
Vlad L

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

Nowhere Man
Nowhere Man

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

Related Questions