public final class ConcurrentCollectors extends Object
Collectors that can be used
in conjunction with the Java Stream API. These collectors allow for thread-safe
accumulation of elements into collections or other aggregate results.| Modifier and Type | Method and Description |
|---|---|
static <T> @NotNull Collector<T,?,Optional<T>> |
reducingConcurrent(@NotNull BinaryOperator<T> op)
Returns a
Collector which performs a concurrent reduction of its
input elements under a specified BinaryOperator. |
static <T,R> @NotNull Collector<T,?,R> |
reducingConcurrent(R identity,
@NotNull Function<? super T,? extends R> mapper,
@NotNull BinaryOperator<R> op)
Returns a
Collector which performs a concurrent reduction of its
input elements under a specified mapping function and
BinaryOperator. |
static <T> Collector<T,?,T> |
reducingConcurrent(T identity,
@NotNull BinaryOperator<T> op)
Returns a
Collector which performs a concurrent reduction of its
input elements under a specified BinaryOperator using the
provided identity. |
static <V> BinaryOperator<V> |
replacingMerger()
Returns a merger that will replace an existing value with the latest value.
|
static <V> BinaryOperator<V> |
retainingMerger()
Returns a merger that will retain an existing value and discard the latest value.
|
static <V> BinaryOperator<V> |
throwingMerger()
Returns a merger that will throw an Exception if duplicate keys are detected.
|
static <T> @NotNull Collector<T,?,List<T>> |
toConcurrentList()
Returns a concurrent
Collector that reduces the input elements into a
new List. |
static <T> @NotNull Collector<T,?,Set<T>> |
toConcurrentSet()
Returns a concurrent
Collector that reduces the input elements into a
new Set. |
@NotNull public static <T> @NotNull Collector<T,?,List<T>> toConcurrentList()
Collector that reduces the input elements into a
new List.T - the type of the input elementsCollector which collects all the input elements into a
List, in encounter order@NotNull public static <T> @NotNull Collector<T,?,Set<T>> toConcurrentSet()
Collector that reduces the input elements into a
new Set.
This is an unordered
Collector.
T - the type of the input elementsCollector which collects all the input elements into a
Setpublic static <T> Collector<T,?,T> reducingConcurrent(T identity, @NotNull @NotNull BinaryOperator<T> op)
Collector which performs a concurrent reduction of its
input elements under a specified BinaryOperator using the
provided identity.
Note: The reducing() collectors are most useful when used in a
multi-level reduction, downstream of groupingBy or
partitioningBy.
T - element type for the input and output of the reductionidentity - the identity value for the reduction (also, the value
that is returned when there are no input elements)op - a BinaryOperator<T> used to reduce the input elementsCollector which implements the reduction operationCollectors.reducing(Object, BinaryOperator)@NotNull public static <T> @NotNull Collector<T,?,Optional<T>> reducingConcurrent(@NotNull @NotNull BinaryOperator<T> op)
Collector which performs a concurrent reduction of its
input elements under a specified BinaryOperator. The result
is described as an Optional<T>.
For example, given a stream of Person, to calculate tallest
person in each city:
Comparator<Person> byHeight = Comparator.comparing(Person::getHeight);
Map<City, Person> tallestByCity
= people.stream().collect(groupingBy(Person::getCity, reducing(BinaryOperator.maxBy(byHeight))));
T - element type for the input and output of the reductionop - a BinaryOperator<T> used to reduce the input elementsCollector which implements the reduction operationCollectors.reducing(BinaryOperator)@NotNull public static <T,R> @NotNull Collector<T,?,R> reducingConcurrent(@Nullable R identity, @NotNull @NotNull Function<? super T,? extends R> mapper, @NotNull @NotNull BinaryOperator<R> op)
Collector which performs a concurrent reduction of its
input elements under a specified mapping function and
BinaryOperator. This is a generalization of
Collectors.reducing(Object, BinaryOperator) which allows a transformation
of the elements before reduction.
Note: The reducing() collectors are most useful when used in a
multi-level reduction, downstream of groupingBy or
partitioningBy. To perform a simple map-reduce on a stream,
use Stream.map(Function) and Stream.reduce(Object, BinaryOperator)
instead.
T - the type of the input elementsR - the type of the mapped valuesidentity - the identity value for the reduction (also, the value
that is returned when there are no input elements)mapper - a mapping function to apply to each input valueop - a BinaryOperator<U> used to reduce the mapped valuesCollector implementing the map-reduce operation
For example, given a stream of Person, to calculate the longest
last name of residents in each city:
Comparator<String> byLength = Comparator.comparing(String::length);
Map<City, String> longestLastNameByCity
= people.stream().collect(groupingBy(Person::getCity,
reducing(Person::getLastName, BinaryOperator.maxBy(byLength))));
Collectors.reducing(Object, Function, BinaryOperator)public static <V> BinaryOperator<V> replacingMerger()
V - value typepublic static <V> BinaryOperator<V> retainingMerger()
V - value typepublic static <V> BinaryOperator<V> throwingMerger()
V - value typeCopyright © 2024. All rights reserved.