Close

Java 8 Streams - Collectors.groupingByConcurrent Examples

Java 8 Streams Java Java API 


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

The overloaded Collectors#groupingByConcurrent() methods perform group by operation on the input stream elements. They use a concurrent container (ConcurrentMap) to achieve that. The collector created from these methods, has CONCURRENT and UNORDERED characteristics, that means, the collector optimizes the performance by not using combiner function instead directly updates the concurrent map. It also ignores the encounter order of the stream elements.


Followings are the overloaded groupingByConcurrent() methods:

<T,K> Collector<T,?,ConcurrentMap<K,List<T>>> groupingByConcurrent(

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

<T,K,A,D> Collector<T,?,ConcurrentMap<K,D>> groupingByConcurrent(

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

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

<T,K,A,D,M extends ConcurrentMap<K,D>> Collector<T,?,M> groupingByConcurrent(

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

Supplier<M> mapFactory,

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


This examples shows how to use method groupingByConcurrent(classifier) to collect stream element to a ConcurrentMap. The keys of the map are populated per provided classifier function returned values.

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 GroupingByConcurrent {
public static void main (String[] args) {
Stream<String> s = Stream.of("apple", "banana", "orange");
Map<Integer, List<String>> map = s.collect(
Collectors.groupingByConcurrent(String::length));
System.out.println(map);
}
}

Output

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




This examples shows how to use two parameter method groupingByConcurrent(classifier, downstream).

package com.logicbig.example.collectors;

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

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

Output

{5=1, 6=2}
Original Post




This examples shows how to use method groupingByConcurrent(classifier,  mapFactory,  downstream) to collect stream element to a ConcurrentMap. The classifier function (first argument) maps the input stream elements to the target map keys. The second argument is a map factory. In the following examples we are specifying ConcurrentSkipListMap to be the mutable container. The third parameter performs a reduction operation on the values of associated with the map keys.

package com.logicbig.example.collectors;

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

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

Output

{5=1, 6=2}
Original Post




See Also