0

I have this code:

    List<String> list = Arrays.asList("a", "b", "c", "a", "c");
    Map<String, List<String>> map = list.stream().collect(Collectors.groupingBy(
    Function.identity() ,
    Collectors.toCollection(ArrayList::new)
    ));

what it produces is

  {a=[a, a], b=[b], c=[c, c]}

Pretty please how should I write mapping method to get a Map that would give me:

{a=2, b=1, c=2}
J.Doe
  • 287
  • 1
  • 5
  • 16

1 Answers1

3

Use Collectors.counting

list.stream().collect(Collectors.groupingBy(
            Function.identity(), Collectors.counting()));

The above would give you a Map<String, Long>

Thiyagu
  • 17,362
  • 5
  • 42
  • 79