Close

Java 8 Streams - Collectors.groupingBy Examples

Java 8 Streams Java Java API 


java.lang.Objectjava.lang.Objectjava.util.stream.Collectorsjava.util.stream.CollectorsLogicBig

The overloaded static methods, Collector#groupingBy() return a Collector which maps each element of the stream to a map entries.

<T,K> Collector<T,?,Map<K,List<T>>> groupingBy(

Function<? super T,? extends K> classifier)

<T,K,A,D> Collector<T,?,Map<K,D>> groupingBy(

Function<? super T,? extends K> classifier,

Collector<? super T,A,D> downstream)

<T,K,D,A,M extends Map<K,D>> Collector<T,?,M> groupingBy(

Function<? super T,? extends K> classifier,

Supplier<M> mapFactory,

Collector<? super T,A,D> downstream)

Parameters

classifier: This function returned value is used as map key. Inputs to this function are stream elements.

downstream: This collector transforms the map values to type D.

mapFactory: This function creates the desired Map implementation.


Examples


This example uses groupingBy(classifier) method and converts the stream string elements to a map having keys as length of input strings and values as input strings.

package com.logicbig.example.collectors;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;


public class GroupingByExample {
public static void main (String[] args) {
Stream<String> s = Stream.of("apple", "banana", "orange");
Map<Integer, List<String>> map = s.collect(
Collectors.groupingBy(String::length));
System.out.println(map);
}
}

Output

{5=[apple], 6=[banana, orange]}
Original Post




This example uses groupingBy(classifier, downstream) method . It converts the stream string elements to a map having keys as length of input strings and values as number of occurrence of elements.

package com.logicbig.example.collectors;

import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* Created by Joe on 11/27/2016.
*/
public class GroupingByExample2 {
public static void main (String[] args) {
Stream<String> s = Stream.of("apple", "banana", "orange");
//cascaded group by
Map<Integer, Long> map = s.collect(
Collectors.groupingBy(String::length, Collectors.counting()));
System.out.println(map);
}
}

Output

{5=1, 6=2}
Original Post




This example uses groupingBy(classifier, mapFactory, downstream) and converts the stream string elements to ConcurrentHashMap having keys as length of input strings and values as number of occurrence of elements.

package com.logicbig.example.collectors;

import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class GroupingByExample3 {
public static void main (String[] args) {
Stream<String> s = Stream.of("apple", "banana", "orange");
//cascaded group by
ConcurrentHashMap<Integer, Long> map = s.collect(
Collectors.groupingBy(String::length,
ConcurrentHashMap::new,
Collectors.counting()));
System.out.println(map);
}
}

Output

{5=1, 6=2}
Original Post




See Also