Java streams 30. Collect 6. Collectors.groupingBy()

Collectors.groupingBy()

  • Collector<T,?,Map<K,List<T>>> groupingBy(Function<T,K> classifier) — returns a Collector implementing a “group by” operation on input elements of type T, grouping elements according to a classification function, and returning the results in a Map;
  • Collector<T,?,Map<K,D>> groupingBy(Function<T,K> classifier, Collector<T,A,D> downstream) — returns a Collector implementing a cascaded “group by” operation on input elements of type T, grouping elements according to a classification function, and then performing a reduction operation on the values associated with a given key using the specified downstream Collector;
  • Collector<T,?,M> groupingBy(Function<T,K> classifier, Supplier<M> mapSupplier, Collector<T,A,D> downstream) — returns a Collector implementing a cascaded “group by” operation on input elements of type T, grouping elements according to a classification function, and then performing a reduction operation on the values associated with a given key using the specified downstream Collector.
Map<String, List<String>> map1 = 
Stream.of("cat", "fish", "cat", "dog")
.collect(Collectors.groupingBy(Function.identity()));
System.out.print(map1);
//prints: {fish=[fish], cat=[cat, cat], dog=[dog]}
Map<Integer, List<String>> map2 =
Stream.of("cat", "fish", "cat", "dog")
.collect(Collectors.groupingBy(String::length));
System.out.print(map2); //prints: {3=[cat, cat, dog], 4=[fish]}
Map<Integer, Set<String>> map3 =
Stream.of("cat", "fish", "cat", "dog")
.collect(Collectors.groupingBy(String::length,
Collectors.toSet()));
System.out.print(map3); //prints: {3=[cat, dog], 4=[fish]}
Map<String, Long> map4 =
Stream.of("cat", "fish", "cat", "dog")
.collect(Collectors.groupingBy(Function.identity(),
Collectors.counting()));
System.out.print(map4); //prints: {fish=1, cat=2, dog=1}
Map<Integer, Map<String, List<String>>> map5 =
Stream.of("cat", "fish", "cat", "dog")
.collect(Collectors.groupingBy(String::length,
Collectors.groupingBy(Function.identity())));
System.out.print(map5);
//prints: {3={cat=[cat, cat], dog=[dog]}, 4={fish=[fish]}}
Map<Integer, Map<String, Long>> map6 =
Stream.of("cat", "fish", "cat", "dog")
.collect(Collectors.groupingBy(String::length,
Collectors.groupingBy(Function.identity(),
Collectors.counting())));
System.out.print(map6);
//prints: {3={cat=2, dog=1}, 4={fish=1}}
ConcurrentMap<Integer, Map<String, Long>> map7 =
Stream.of("cat", "fish", "cat", "dog")
.collect(Collectors.groupingBy(String::length,
ConcurrentHashMap::new,
Collectors.groupingBy(Function.identity(),
Collectors.counting())));
System.out.print(map7);
//prints: {3={cat=2, dog=1}, 4={fish=1}}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Nick Samoylov

Nick Samoylov

Born in Moscow, lived in Crimea, now lives in the US. Used to be physicist and rock climber, now programmer and writer.