public class Collectors
extends java.lang.Object
Collector
that implement various useful reduction
operations, such as accumulating elements into collections, summarizing
elements according to various criteria, etc.
The following are examples of using the predefined collectors to perform common mutable reduction tasks:
// Accumulate names into a List
List<String> list = people.stream().map(Person::getName).collect(Collectors.toList());
// Accumulate names into a TreeSet
Set<String> set = people.stream().map(Person::getName).collect(Collectors.toCollection(TreeSet::new));
// Convert elements to strings and concatenate them, separated by commas
String joined = things.stream()
.map(Object::toString)
.collect(Collectors.joining(", "));
// Compute sum of salaries of employee
int total = employees.stream()
.collect(Collectors.summingInt(Employee::getSalary)));
// Group employees by department
Map<Department, List<Employee>> byDept
= employees.stream()
.collect(Collectors.groupingBy(Employee::getDepartment));
// Compute sum of salaries by department
Map<Department, Integer> totalByDept
= employees.stream()
.collect(Collectors.groupingBy(Employee::getDepartment,
Collectors.summingInt(Employee::getSalary)));
// Partition students into passing and failing
Map<Boolean, List<Student>> passingFailing =
students.stream()
.collect(Collectors.partitioningBy(s -> s.getGrade() >= PASS_THRESHOLD));
Modifier and Type | Class and Description |
---|---|
static class |
Collectors.MoreCollectors |
Modifier and Type | Method and Description |
---|---|
static <T> Collector<T,?,java.lang.Double> |
averagingDouble(ToDoubleFunction<? super T> mapper)
Returns a
Collector that produces the arithmetic mean of a double-valued
function applied to the input elements. |
static <T> Collector<T,?,OptionalDouble> |
averagingDouble2(ToDoubleFunction<? super T> mapper) |
static <T> Collector<T,?,java.lang.Double> |
averagingInt(ToIntFunction<? super T> mapper)
Returns a
Collector that produces the arithmetic mean of an integer-valued
function applied to the input elements. |
static <T> Collector<T,?,OptionalDouble> |
averagingInt2(ToIntFunction<? super T> mapper) |
static <T> Collector<T,?,java.lang.Double> |
averagingLong(ToLongFunction<? super T> mapper)
Returns a
Collector that produces the arithmetic mean of a long-valued
function applied to the input elements. |
static <T> Collector<T,?,OptionalDouble> |
averagingLong2(ToLongFunction<? super T> mapper) |
static <T,A,R,RR> Collector<T,A,RR> |
collectingAndThen(Collector<T,A,R> downstream,
Function<R,RR> finisher)
Adapts a
Collector to perform an additional finishing
transformation. |
static Collector<java.lang.CharSequence,?,java.lang.String> |
commonPrefix()
It's copied from StreamEx: https://github.com/amaembo/streamex
Returns a Collector which computes a common prefix of input
CharSequence objects returning the result as String . |
static Collector<java.lang.CharSequence,?,java.lang.String> |
commonSuffix()
It's copied from StreamEx: https://github.com/amaembo/streamex
Returns a Collector which computes a common suffix of input
CharSequence objects returning the result as String . |
static <T> Collector<T,?,java.lang.Long> |
counting()
Returns a
Collector accepting elements of type T that
counts the number of input elements. |
static <T> Collector<T,?,java.lang.Integer> |
countingInt() |
static <T> Collector<T,?,java.util.List<T>> |
distinctBy(Function<? super T,?> mapper)
Returns a
Collector which collects into the List the
input elements for which given mapper function returns distinct results. |
static <T> Collector<T,?,java.lang.Integer> |
distinctCount(Function<? super T,?> mapper)
It's copied from StreamEx: https://github.com/amaembo/streamex
Returns a Collector which counts a number of distinct values the
mapper function returns for the stream elements. |
static <T> Collector<T,?,java.util.List<T>> |
dominators(BiPredicate<? super T,? super T> isDominator)
It's copied from StreamEx: https://github.com/amaembo/streamex
Returns a collector which collects input elements into List
removing the elements following their dominator element. |
static <T> Collector<T,?,java.util.List<T>> |
filtering(Predicate<? super T> predicate)
It's copied from StreamEx: https://github.com/amaembo/streamex
Returns a Collector which filters input elements by the supplied
predicate, collecting them to the list. |
static <T,A,R> Collector<T,?,R> |
filtering(Predicate<? super T> predicate,
Collector<? super T,A,R> downstream)
It's copied from StreamEx: https://github.com/amaembo/streamex
Returns a Collector which passes only those elements to the
specified downstream collector which match given predicate. |
static <T,U> Collector<T,?,java.util.List<U>> |
flatMapping(Function<? super T,? extends Stream<? extends U>> mapper) |
static <T,U,A,R> Collector<T,?,R> |
flatMapping(Function<? super T,? extends Stream<? extends U>> mapper,
Collector<? super U,A,R> downstream) |
static <T,K> Collector<T,?,java.util.Map<K,java.util.List<T>>> |
groupingBy(Function<? super T,? extends 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 . |
static <T,K,A,D> Collector<T,?,java.util.Map<K,D>> |
groupingBy(Function<? super T,? extends K> classifier,
Collector<? super 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 . |
static <T,K,A,D,M extends java.util.Map<K,D>> |
groupingBy(Function<? super T,? extends K> classifier,
Collector<? super T,A,D> downstream,
Supplier<M> mapFactory)
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 . |
static <T,K,M extends java.util.Map<K,java.util.List<T>>> |
groupingBy(Function<? super T,? extends K> classifier,
Supplier<M> mapFactory) |
static <T,K> Collector<T,?,java.util.concurrent.ConcurrentMap<K,java.util.List<T>>> |
groupingByConcurrent(Function<? super T,? extends K> classifier)
Returns a concurrent
Collector implementing a "group by"
operation on input elements of type T , grouping elements
according to a classification function. |
static <T,K,A,D> Collector<T,?,java.util.concurrent.ConcurrentMap<K,D>> |
groupingByConcurrent(Function<? super T,? extends K> classifier,
Collector<? super T,A,D> downstream)
Returns a concurrent
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 . |
static <T,K,A,D,M extends java.util.concurrent.ConcurrentMap<K,D>> |
groupingByConcurrent(Function<? super T,? extends K> classifier,
Collector<? super T,A,D> downstream,
Supplier<M> mapFactory)
Returns a concurrent
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 . |
static <T,K,M extends java.util.concurrent.ConcurrentMap<K,java.util.List<T>>> |
groupingByConcurrent(Function<? super T,? extends K> classifier,
Supplier<M> mapFactory) |
static Collector<java.lang.CharSequence,?,java.lang.String> |
joining()
Returns a
Collector that concatenates the input elements into a
String , in encounter order. |
static Collector<java.lang.CharSequence,?,java.lang.String> |
joining(java.lang.CharSequence delimiter)
Returns a
Collector that concatenates the input elements,
separated by the specified delimiter, in encounter order. |
static Collector<java.lang.CharSequence,?,java.lang.String> |
joining(java.lang.CharSequence delimiter,
java.lang.CharSequence prefix,
java.lang.CharSequence suffix)
Returns a
Collector that concatenates the input elements,
separated by the specified delimiter, with the specified prefix and
suffix, in encounter order. |
static <T> Collector<T,?,java.util.List<T>> |
last(int n) |
static <T,U> Collector<T,?,java.util.List<U>> |
mapping(Function<? super T,? extends U> mapper) |
static <T,U,A,R> Collector<T,?,R> |
mapping(Function<? super T,? extends U> mapper,
Collector<? super U,A,R> downstream)
Adapts a
Collector accepting elements of type U to one
accepting elements of type T by applying a mapping function to
each input element before accumulation. |
static <T extends java.lang.Comparable> |
max() |
static <T extends java.lang.Comparable> |
maxAll()
It's copied from StreamEx: https://github.com/amaembo/streamex
Returns a Collector which finds all the elements which are equal
to each other and bigger than any other element according to the natural
order. |
static <T extends java.lang.Comparable,A,D> |
maxAll(Collector<T,A,D> downstream)
It's copied from StreamEx: https://github.com/amaembo/streamex
Returns a Collector which finds all the elements which are equal
to each other and bigger than any other element according to the natural
order. |
static <T> Collector<T,?,java.util.List<T>> |
maxAll(java.util.Comparator<? super T> comparator)
It's copied from StreamEx: https://github.com/amaembo/streamex
Returns a Collector which finds all the elements which are equal
to each other and bigger than any other element according to the
specified Comparator . |
static <T,A,D> Collector<T,?,D> |
maxAll(java.util.Comparator<? super T> comparator,
Collector<? super T,A,D> downstream)
It's copied from StreamEx: https://github.com/amaembo/streamex
Returns a Collector which finds all the elements which are equal
to each other and bigger than any other element according to the
specified Comparator . |
static <T> Collector<T,?,java.util.List<T>> |
maxAll(java.util.Comparator<? super T> comparator,
int atMostSize) |
static <T> Collector<T,?,Nullable<T>> |
maxBy(java.util.Comparator<? super T> comparator)
Returns a
Collector that produces the maximal element according
to a given Comparator , described as an Nullable<T> . |
static <T> Collector<T,?,T> |
maxByOrGet(java.util.Comparator<? super T> comparator,
Supplier<? extends T> other) |
static <T,X extends java.lang.RuntimeException> |
maxByOrThrow(java.util.Comparator<? super T> comparator,
Supplier<? extends X> exceptionSupplier) |
static <T extends java.lang.Comparable> |
min() |
static <T extends java.lang.Comparable> |
minAll()
It's copied from StreamEx: https://github.com/amaembo/streamex
Returns a Collector which finds all the elements which are equal
to each other and smaller than any other element according to the natural
order. |
static <T extends java.lang.Comparable,A,D> |
minAll(Collector<T,A,D> downstream)
It's copied from StreamEx: https://github.com/amaembo/streamex
Returns a Collector which finds all the elements which are equal
to each other and smaller than any other element according to the natural
order. |
static <T> Collector<T,?,java.util.List<T>> |
minAll(java.util.Comparator<? super T> comparator)
It's copied from StreamEx: https://github.com/amaembo/streamex
Returns a Collector which finds all the elements which are equal
to each other and smaller than any other element according to the
specified Comparator . |
static <T,A,D> Collector<T,?,D> |
minAll(java.util.Comparator<? super T> comparator,
Collector<T,A,D> downstream)
It's copied from StreamEx: https://github.com/amaembo/streamex
Returns a Collector which finds all the elements which are equal
to each other and smaller than any other element according to the
specified Comparator . |
static <T> Collector<T,?,java.util.List<T>> |
minAll(java.util.Comparator<? super T> comparator,
int atMostSize) |
static <T> Collector<T,?,Nullable<T>> |
minBy(java.util.Comparator<? super T> comparator)
Returns a
Collector that produces the minimal element according
to a given Comparator , described as an Nullable<T> . |
static <T> Collector<T,?,T> |
minByOrGet(java.util.Comparator<? super T> comparator,
Supplier<? extends T> other) |
static <T,X extends java.lang.RuntimeException> |
minByOrThrow(java.util.Comparator<? super T> comparator,
Supplier<? extends X> exceptionSupplier) |
static <T,R> Collector<T,?,R> |
minMax(java.util.Comparator<? super T> comparator,
BiFunction<? super Nullable<T>,? super Nullable<T>,? extends R> finisher)
It's copied from StreamEx: https://github.com/amaembo/streamex
Returns a Collector which finds the minimal and maximal element
according to the supplied comparator, then applies finisher function to
them producing the final result. |
static <T,A1,A2,R1,R2,R> |
pairing(Collector<? super T,A1,R1> c1,
Collector<? super T,A2,R2> c2,
BiFunction<? super R1,? super R2,? extends R> finisher)
It's copied from StreamEx: https://github.com/amaembo/streamex
Returns a Collector which aggregates the results of two supplied
collectors using the supplied finisher function. |
static <T> Collector<T,?,java.util.Map<java.lang.Boolean,java.util.List<T>>> |
partitioningBy(Predicate<? super T> predicate)
Returns a
Collector which partitions the input elements according
to a Predicate , and organizes them into a
Map<Boolean, List<T>> . |
static <T,D,A> Collector<T,?,java.util.Map<java.lang.Boolean,D>> |
partitioningBy(Predicate<? super T> predicate,
Collector<? super T,A,D> downstream)
Returns a
Collector which partitions the input elements according
to a Predicate , reduces the values in each partition according to
another Collector , and organizes them into a
Map<Boolean, D> whose values are the result of the downstream
reduction. |
static <T> Collector<T,?,Nullable<T>> |
reducing(BinaryOperator<T> op)
Returns a
Collector which performs a reduction of its
input elements under a specified BinaryOperator . |
static <T,U> Collector<T,?,Nullable<U>> |
reducing(Function<? super T,? extends U> mapper,
BinaryOperator<U> op) |
static <T> Collector<T,?,T> |
reducing(T identity,
BinaryOperator<T> op)
Returns a
Collector which performs a reduction of its
input elements under a specified BinaryOperator using the
provided identity. |
static <T,U> Collector<T,?,U> |
reducing(U identity,
Function<? super T,? extends U> mapper,
BinaryOperator<U> op)
Returns a
Collector which performs a reduction of its
input elements under a specified mapping function and
BinaryOperator . |
static <T> Collector<T,?,T> |
reducingOrGet(BinaryOperator<T> op,
Supplier<? extends T> other) |
static <T,U> Collector<T,?,U> |
reducingOrGet(Function<? super T,? extends U> mapper,
BinaryOperator<U> op,
Supplier<? extends U> other) |
static <T,X extends java.lang.RuntimeException> |
reducingOrThrow(BinaryOperator<T> op,
Supplier<? extends X> exceptionSupplier) |
static <T,U,X extends java.lang.RuntimeException> |
reducingOrThrow(Function<? super T,? extends U> mapper,
BinaryOperator<U> op,
Supplier<? extends X> exceptionSupplier) |
static <T> Collector<T,?,com.landawn.abacus.util.ByteSummaryStatistics> |
summarizingByte(ToByteFunction<? super T> mapper) |
static <T> Collector<T,?,com.landawn.abacus.util.CharSummaryStatistics> |
summarizingChar(ToCharFunction<? super T> mapper) |
static <T> Collector<T,?,DoubleSummaryStatistics> |
summarizingDouble(ToDoubleFunction<? super T> mapper)
Returns a
Collector which applies an double -producing
mapping function to each input element, and returns summary statistics
for the resulting values. |
static <T> Collector<T,?,com.landawn.abacus.util.FloatSummaryStatistics> |
summarizingFloat(ToFloatFunction<? super T> mapper) |
static <T> Collector<T,?,IntSummaryStatistics> |
summarizingInt(ToIntFunction<? super T> mapper)
Returns a
Collector which applies an int -producing
mapping function to each input element, and returns summary statistics
for the resulting values. |
static <T> Collector<T,?,LongSummaryStatistics> |
summarizingLong(ToLongFunction<? super T> mapper)
Returns a
Collector which applies an long -producing
mapping function to each input element, and returns summary statistics
for the resulting values. |
static <T> Collector<T,?,com.landawn.abacus.util.ShortSummaryStatistics> |
summarizingShort(ToShortFunction<? super T> mapper) |
static <T> Collector<T,?,java.lang.Double> |
summingDouble(ToDoubleFunction<? super T> mapper)
Returns a
Collector that produces the sum of a double-valued
function applied to the input elements. |
static <T> Collector<T,?,OptionalDouble> |
summingDouble2(ToDoubleFunction<? super T> mapper) |
static <T> Collector<T,?,java.lang.Integer> |
summingInt(ToIntFunction<? super T> mapper)
Returns a
Collector that produces the sum of a integer-valued
function applied to the input elements. |
static <T> Collector<T,?,OptionalInt> |
summingInt2(ToIntFunction<? super T> mapper) |
static <T> Collector<T,?,java.lang.Long> |
summingLong(ToLongFunction<? super T> mapper)
Returns a
Collector that produces the sum of a long-valued
function applied to the input elements. |
static <T> Collector<T,?,OptionalLong> |
summingLong2(ToLongFunction<? super T> mapper) |
static <T> Collector<T,?,java.lang.Object[]> |
toArray() |
static <T,A> Collector<T,?,A[]> |
toArray(Supplier<A[]> arraySupplier) |
static <T,K,U> Collector<T,?,BiMap<K,U>> |
toBiMap(Function<? super T,? extends K> keyExtractor,
Function<? super T,? extends U> valueMapper) |
static <T,K,U> Collector<T,?,BiMap<K,U>> |
toBiMap(Function<? super T,? extends K> keyExtractor,
Function<? super T,? extends U> valueMapper,
BinaryOperator<U> mergeFunction) |
static <T,K,U> Collector<T,?,BiMap<K,U>> |
toBiMap(Function<? super T,? extends K> keyExtractor,
Function<? super T,? extends U> valueMapper,
BinaryOperator<U> mergeFunction,
Supplier<BiMap<K,U>> mapFactory) |
static <T,K,U> Collector<T,?,BiMap<K,U>> |
toBiMap(Function<? super T,? extends K> keyExtractor,
Function<? super T,? extends U> valueMapper,
Supplier<BiMap<K,U>> mapFactory) |
static Collector<java.lang.Boolean,?,boolean[]> |
toBooleanArray() |
static Collector<java.lang.Boolean,?,BooleanList> |
toBooleanList() |
static Collector<java.lang.Byte,?,byte[]> |
toByteArray() |
static Collector<java.lang.Byte,?,ByteList> |
toByteList() |
static Collector<java.lang.Character,?,char[]> |
toCharArray() |
static Collector<java.lang.Character,?,CharList> |
toCharList() |
static <T,C extends java.util.Collection<T>> |
toCollection(Supplier<C> collectionFactory)
Returns a
Collector that accumulates the input elements into a
new Collection , in encounter order. |
static <T,C extends java.util.Collection<T>> |
toCollection(Supplier<C> collectionFactory,
int atMostSize) |
static <T,K,U> Collector<T,?,java.util.concurrent.ConcurrentMap<K,U>> |
toConcurrentMap(Function<? super T,? extends K> keyExtractor,
Function<? super T,? extends U> valueMapper)
Returns a concurrent
Collector that accumulates elements into a
ConcurrentMap whose keys and values are the result of applying
the provided mapping functions to the input elements. |
static <T,K,U> Collector<T,?,java.util.concurrent.ConcurrentMap<K,U>> |
toConcurrentMap(Function<? super T,? extends K> keyExtractor,
Function<? super T,? extends U> valueMapper,
BinaryOperator<U> mergeFunction)
Returns a concurrent
Collector that accumulates elements into a
ConcurrentMap whose keys and values are the result of applying
the provided mapping functions to the input elements. |
static <T,K,U,M extends java.util.concurrent.ConcurrentMap<K,U>> |
toConcurrentMap(Function<? super T,? extends K> keyExtractor,
Function<? super T,? extends U> valueMapper,
BinaryOperator<U> mergeFunction,
Supplier<M> mapFactory)
Returns a concurrent
Collector that accumulates elements into a
ConcurrentMap whose keys and values are the result of applying
the provided mapping functions to the input elements. |
static <T,K,U,M extends java.util.concurrent.ConcurrentMap<K,U>> |
toConcurrentMap(Function<? super T,? extends K> keyExtractor,
Function<? super T,? extends U> valueMapper,
Supplier<M> mapFactory) |
static <T> Collector<T,?,DataSet> |
toDataSet() |
static <T> Collector<T,?,DataSet> |
toDataSet(java.util.List<java.lang.String> columnNames) |
static <T> Collector<T,?,java.util.Deque<T>> |
toDeque() |
static Collector<java.lang.Double,?,double[]> |
toDoubleArray() |
static Collector<java.lang.Double,?,DoubleList> |
toDoubleList() |
static Collector<java.lang.Float,?,float[]> |
toFloatArray() |
static Collector<java.lang.Float,?,FloatList> |
toFloatList() |
static <T> Collector<T,?,ImmutableList<T>> |
toImmutableList() |
static <K,V> Collector<java.util.Map.Entry<K,V>,?,ImmutableMap<K,V>> |
toImmutableMap() |
static <K,V> Collector<java.util.Map.Entry<K,V>,?,ImmutableMap<K,V>> |
toImmutableMap(BinaryOperator<V> mergeFunction) |
static <T,K,U> Collector<T,?,ImmutableMap<K,U>> |
toImmutableMap(Function<? super T,? extends K> keyExtractor,
Function<? super T,? extends U> valueMapper) |
static <T,K,U> Collector<T,?,ImmutableMap<K,U>> |
toImmutableMap(Function<? super T,? extends K> keyExtractor,
Function<? super T,? extends U> valueMapper,
BinaryOperator<U> mergeFunction) |
static <T> Collector<T,?,ImmutableSet<T>> |
toImmutableSet() |
static Collector<java.lang.Integer,?,int[]> |
toIntArray() |
static Collector<java.lang.Integer,?,IntList> |
toIntList() |
static <T,K,U> Collector<T,?,java.util.LinkedHashMap<K,U>> |
toLinkedHashMap(Function<? super T,? extends K> keyExtractor,
Function<? super T,? extends U> valueMapper) |
static <T,K,U> Collector<T,?,java.util.LinkedHashMap<K,U>> |
toLinkedHashMap(Function<? super T,? extends K> keyExtractor,
Function<? super T,? extends U> valueMapper,
BinaryOperator<U> mergeFunction) |
static <T> Collector<T,?,java.util.LinkedHashSet<T>> |
toLinkedHashSet() |
static <T> Collector<T,?,java.util.LinkedList<T>> |
toLinkedList() |
static <T> Collector<T,?,java.util.List<T>> |
toList()
Returns a
Collector that accumulates the input elements into a
new List . |
static <T> Collector<T,?,java.util.List<T>> |
toList(int atMostSize) |
static Collector<java.lang.Long,?,long[]> |
toLongArray() |
static Collector<java.lang.Long,?,LongList> |
toLongList() |
static <T> Collector<T,?,LongMultiset<T>> |
toLongMultiset() |
static <T> Collector<T,?,LongMultiset<T>> |
toLongMultiset(Supplier<LongMultiset<T>> supplier) |
static <K,V> Collector<java.util.Map.Entry<K,V>,?,java.util.Map<K,V>> |
toMap() |
static <K,V> Collector<java.util.Map.Entry<K,V>,?,java.util.Map<K,V>> |
toMap(BinaryOperator<V> mergeFunction) |
static <K,V,M extends java.util.Map<K,V>> |
toMap(BinaryOperator<V> mergeFunction,
Supplier<M> mapFactory) |
static <T,K,U> Collector<T,?,java.util.Map<K,U>> |
toMap(Function<? super T,? extends K> keyExtractor,
Function<? super T,? extends U> valueMapper)
Returns a
Collector that accumulates elements into a
Map whose keys and values are the result of applying the provided
mapping functions to the input elements. |
static <T,K,U> Collector<T,?,java.util.Map<K,U>> |
toMap(Function<? super T,? extends K> keyExtractor,
Function<? super T,? extends U> valueMapper,
BinaryOperator<U> mergeFunction)
Returns a
Collector that accumulates elements into a
Map whose keys and values are the result of applying the provided
mapping functions to the input elements. |
static <T,K,U,M extends java.util.Map<K,U>> |
toMap(Function<? super T,? extends K> keyExtractor,
Function<? super T,? extends U> valueMapper,
BinaryOperator<U> mergeFunction,
Supplier<M> mapFactory)
Returns a
Collector that accumulates elements into a
Map whose keys and values are the result of applying the provided
mapping functions to the input elements. |
static <T,K,U,M extends java.util.Map<K,U>> |
toMap(Function<? super T,? extends K> keyExtractor,
Function<? super T,? extends U> valueMapper,
Supplier<M> mapFactory) |
static <K,V,M extends java.util.Map<K,V>> |
toMap(Supplier<M> mapFactory) |
static <K,E> Collector<java.util.Map.Entry<? extends K,? extends E>,?,ListMultimap<K,E>> |
toMultimap() |
static <T,K> Collector<T,?,ListMultimap<K,T>> |
toMultimap(Function<? super T,? extends K> keyExtractor) |
static <T,K,U> Collector<T,?,ListMultimap<K,U>> |
toMultimap(Function<? super T,? extends K> keyExtractor,
Function<? super T,? extends U> valueMapper) |
static <T,K,U,V extends java.util.Collection<U>,M extends Multimap<K,U,V>> |
toMultimap(Function<? super T,? extends K> keyExtractor,
Function<? super T,? extends U> valueMapper,
Supplier<M> mapFactory) |
static <T,K,V extends java.util.Collection<T>,M extends Multimap<K,T,V>> |
toMultimap(Function<? super T,? extends K> keyExtractor,
Supplier<M> mapFactory) |
static <K,E,V extends java.util.Collection<E>,M extends Multimap<K,E,V>> |
toMultimap(Supplier<M> mapFactory) |
static <T> Collector<T,?,Multiset<T>> |
toMultiset() |
static <T> Collector<T,?,Multiset<T>> |
toMultiset(Supplier<Multiset<T>> supplier) |
static <T> Collector<T,?,java.util.Queue<T>> |
toQueue() |
static <T> Collector<T,?,java.util.Set<T>> |
toSet()
Returns a
Collector that accumulates the input elements into a
new Set . |
static <T> Collector<T,?,java.util.Set<T>> |
toSet(int atMostSize) |
static Collector<java.lang.Short,?,short[]> |
toShortArray() |
static Collector<java.lang.Short,?,ShortList> |
toShortList() |
public static <T,C extends java.util.Collection<T>> Collector<T,?,C> toCollection(Supplier<C> collectionFactory)
Collector
that accumulates the input elements into a
new Collection
, in encounter order. The Collection
is
created by the provided factory.T
- the type of the input elementsC
- the type of the resulting Collection
collectionFactory
- a Supplier
which returns a new, empty
Collection
of the appropriate typeCollector
which collects all the input elements into a
Collection
, in encounter orderpublic static <T> Collector<T,?,java.util.List<T>> toList()
Collector
that accumulates the input elements into a
new List
. There are no guarantees on the type, mutability,
serializability, or thread-safety of the List
returned; if more
control over the returned List
is required, use toCollection(Supplier)
.T
- the type of the input elementsCollector
which collects all the input elements into a
List
, in encounter orderpublic static <T> Collector<T,?,java.util.LinkedList<T>> toLinkedList()
public static <T> Collector<T,?,ImmutableList<T>> toImmutableList()
public static <T> Collector<T,?,java.util.Set<T>> toSet()
Collector
that accumulates the input elements into a
new Set
. There are no guarantees on the type, mutability,
serializability, or thread-safety of the Set
returned; if more
control over the returned Set
is required, use
toCollection(Supplier)
.
This is an unordered
Collector.
T
- the type of the input elementsCollector
which collects all the input elements into a
Set
public static <T> Collector<T,?,java.util.LinkedHashSet<T>> toLinkedHashSet()
public static <T> Collector<T,?,ImmutableSet<T>> toImmutableSet()
public static <T> Collector<T,?,java.util.Queue<T>> toQueue()
public static <T> Collector<T,?,java.util.Deque<T>> toDeque()
public static <T,C extends java.util.Collection<T>> Collector<T,?,C> toCollection(Supplier<C> collectionFactory, int atMostSize)
public static <T> Collector<T,?,java.util.List<T>> toList(int atMostSize)
public static <T> Collector<T,?,java.util.Set<T>> toSet(int atMostSize)
public static <T> Collector<T,?,LongMultiset<T>> toLongMultiset()
public static <T> Collector<T,?,LongMultiset<T>> toLongMultiset(Supplier<LongMultiset<T>> supplier)
public static <T> Collector<T,?,java.lang.Object[]> toArray()
public static Collector<java.lang.Boolean,?,BooleanList> toBooleanList()
public static Collector<java.lang.Boolean,?,boolean[]> toBooleanArray()
public static Collector<java.lang.Character,?,char[]> toCharArray()
public static Collector<java.lang.Byte,?,byte[]> toByteArray()
public static Collector<java.lang.Short,?,short[]> toShortArray()
public static Collector<java.lang.Integer,?,int[]> toIntArray()
public static Collector<java.lang.Long,?,long[]> toLongArray()
public static Collector<java.lang.Float,?,float[]> toFloatArray()
public static Collector<java.lang.Double,?,DoubleList> toDoubleList()
public static Collector<java.lang.Double,?,double[]> toDoubleArray()
public static <T> Collector<T,?,java.util.List<T>> last(int n)
n
- java.lang.UnsupportedOperationException
- it's used in parallel stream.public static Collector<java.lang.CharSequence,?,java.lang.String> joining()
Collector
that concatenates the input elements into a
String
, in encounter order.Collector
that concatenates the input elements into a
String
, in encounter orderpublic static Collector<java.lang.CharSequence,?,java.lang.String> joining(java.lang.CharSequence delimiter)
Collector
that concatenates the input elements,
separated by the specified delimiter, in encounter order.delimiter
- the delimiter to be used between each elementCollector
which concatenates CharSequence elements,
separated by the specified delimiter, in encounter orderpublic static Collector<java.lang.CharSequence,?,java.lang.String> joining(java.lang.CharSequence delimiter, java.lang.CharSequence prefix, java.lang.CharSequence suffix)
Collector
that concatenates the input elements,
separated by the specified delimiter, with the specified prefix and
suffix, in encounter order.delimiter
- the delimiter to be used between each elementprefix
- the sequence of characters to be used at the beginning
of the joined resultsuffix
- the sequence of characters to be used at the end
of the joined resultCollector
which concatenates CharSequence elements,
separated by the specified delimiter, in encounter orderpublic static <T> Collector<T,?,java.util.List<T>> filtering(Predicate<? super T> predicate)
Collector
which filters input elements by the supplied
predicate, collecting them to the list.
This method behaves like
filtering(predicate, Collectors.toList())
.
There are no guarantees on the type, mutability, serializability, or
thread-safety of the List
returned.
T
- the type of the input elementspredicate
- a filter function to be applied to the input elementsList
filtering(Predicate, Collector)
public static <T,A,R> Collector<T,?,R> filtering(Predicate<? super T> predicate, Collector<? super T,A,R> downstream)
Collector
which passes only those elements to the
specified downstream collector which match given predicate.
This method returns a short-circuiting collector if downstream collector is short-circuiting.
The operation performed by the returned collector is equivalent to
stream.filter(predicate).collect(downstream)
. This collector is
mostly useful as a downstream collector in cascaded operation involving
pairing(Collector, Collector, BiFunction)
collector.
This method is similar to Collectors.filtering
method which
appears in JDK 9. However when downstream collector is
short-circuiting
, this method will also return a short-circuiting collector.
T
- the type of the input elementsA
- intermediate accumulation type of the downstream collectorR
- result type of collectorpredicate
- a filter function to be applied to the input elementsdownstream
- a collector which will accept filtered valuespairing(Collector, Collector, BiFunction)
public static <T,U> Collector<T,?,java.util.List<U>> mapping(Function<? super T,? extends U> mapper)
public static <T,U,A,R> Collector<T,?,R> mapping(Function<? super T,? extends U> mapper, Collector<? super U,A,R> downstream)
Collector
accepting elements of type U
to one
accepting elements of type T
by applying a mapping function to
each input element before accumulation.T
- the type of the input elementsU
- type of elements accepted by downstream collectorA
- intermediate accumulation type of the downstream collectorR
- result type of collectormapper
- a function to be applied to the input elementsdownstream
- a collector which will accept mapped valuespublic static <T,U> Collector<T,?,java.util.List<U>> flatMapping(Function<? super T,? extends Stream<? extends U>> mapper)
public static <T,U,A,R> Collector<T,?,R> flatMapping(Function<? super T,? extends Stream<? extends U>> mapper, Collector<? super U,A,R> downstream)
public static <T,A,R,RR> Collector<T,A,RR> collectingAndThen(Collector<T,A,R> downstream, Function<R,RR> finisher)
Collector
to perform an additional finishing
transformation. For example, one could adapt the toList()
collector to always produce an immutable list with:
List<String> people
= people.stream().collect(collectingAndThen(toList(), Collections::unmodifiableList));
T
- the type of the input elementsA
- intermediate accumulation type of the downstream collectorR
- result type of the downstream collectorRR
- result type of the resulting collectordownstream
- a collectorfinisher
- a function to be applied to the final result of the downstream collectorpublic static <T> Collector<T,?,java.util.List<T>> distinctBy(Function<? super T,?> mapper)
Collector
which collects into the List
the
input elements for which given mapper function returns distinct results.
For ordered source the order of collected elements is preserved. If the same result is returned by mapper function for several elements, only the first element is included into the resulting list.
There are no guarantees on the type, mutability, serializability, or
thread-safety of the List
returned.
The operation performed by the returned collector is equivalent to
stream.distinct(mapper).toList()
, but may work faster.
T
- the type of the input elementsmapper
- a function which classifies input elements.List
.public static <T> Collector<T,?,java.lang.Integer> distinctCount(Function<? super T,?> mapper)
Collector
which counts a number of distinct values the
mapper function returns for the stream elements.
The operation performed by the returned collector is equivalent to
stream.map(mapper).distinct().count()
. This collector is mostly
useful as a downstream collector.
T
- the type of the input elementsmapper
- a function which classifies input elements.public static <T> Collector<T,?,java.lang.Long> counting()
Collector
accepting elements of type T
that
counts the number of input elements. If no elements are present, the
result is 0.T
- the type of the input elementsCollector
that counts the input elementspublic static <T> Collector<T,?,java.lang.Integer> countingInt()
public static <T> Collector<T,?,Nullable<T>> minBy(java.util.Comparator<? super T> comparator)
Collector
that produces the minimal element according
to a given Comparator
, described as an Nullable<T>
.T
- the type of the input elementscomparator
- a Comparator
for comparing elementsCollector
that produces the minimal valuepublic static <T> Collector<T,?,T> minByOrGet(java.util.Comparator<? super T> comparator, Supplier<? extends T> other)
public static <T,X extends java.lang.RuntimeException> Collector<T,?,T> minByOrThrow(java.util.Comparator<? super T> comparator, Supplier<? extends X> exceptionSupplier)
public static <T> Collector<T,?,Nullable<T>> maxBy(java.util.Comparator<? super T> comparator)
Collector
that produces the maximal element according
to a given Comparator
, described as an Nullable<T>
.T
- the type of the input elementscomparator
- a Comparator
for comparing elementsCollector
that produces the maximal valuepublic static <T> Collector<T,?,T> maxByOrGet(java.util.Comparator<? super T> comparator, Supplier<? extends T> other)
public static <T,X extends java.lang.RuntimeException> Collector<T,?,T> maxByOrThrow(java.util.Comparator<? super T> comparator, Supplier<? extends X> exceptionSupplier)
public static <T,A1,A2,R1,R2,R> Collector<T,?,R> pairing(Collector<? super T,A1,R1> c1, Collector<? super T,A2,R2> c2, BiFunction<? super R1,? super R2,? extends R> finisher)
Collector
which aggregates the results of two supplied
collectors using the supplied finisher function.
This method returns a short-circuiting collector if both downstream collectors are short-circuiting. The collection might stop when both downstream collectors report that the collection is complete.
T
- the type of the input elementsA1
- the intermediate accumulation type of the first collectorA2
- the intermediate accumulation type of the second collectorR1
- the result type of the first collectorR2
- the result type of the second collectorR
- the final result typec1
- the first collectorc2
- the second collectorfinisher
- the function which merges two results into the single
one.Collector
which aggregates the results of two supplied
collectors.public static <T,R> Collector<T,?,R> minMax(java.util.Comparator<? super T> comparator, BiFunction<? super Nullable<T>,? super Nullable<T>,? extends R> finisher)
Collector
which finds the minimal and maximal element
according to the supplied comparator, then applies finisher function to
them producing the final result.
This collector produces stable result for ordered stream: if several minimal or maximal elements appear, the collector always selects the first encountered.
If there are no input elements, the finisher method is not called and
empty Optional
is returned. Otherwise the finisher result is
wrapped into Optional
.
T
- the type of the input elementsR
- the type of the result wrapped into Optional
comparator
- comparator which is used to find minimal and maximal
elementfinisher
- a BiFunction
which takes minimal and maximal
element and produces the final result.Collector
which finds minimal and maximal elements.public static <T,A,D> Collector<T,?,D> maxAll(java.util.Comparator<? super T> comparator, Collector<? super T,A,D> downstream)
Collector
which finds all the elements which are equal
to each other and bigger than any other element according to the
specified Comparator
. The found elements are reduced using the
specified downstream Collector
.T
- the type of the input elementsA
- the intermediate accumulation type of the downstream collectorD
- the result type of the downstream reductioncomparator
- a Comparator
to compare the elementsdownstream
- a Collector
implementing the downstream
reductionCollector
which finds all the maximal elements.maxAll(Comparator)
,
maxAll(Collector)
,
maxAll()
public static <T> Collector<T,?,java.util.List<T>> maxAll(java.util.Comparator<? super T> comparator)
Collector
which finds all the elements which are equal
to each other and bigger than any other element according to the
specified Comparator
. The found elements are collected to
List
.T
- the type of the input elementscomparator
- a Comparator
to compare the elementsCollector
which finds all the maximal elements and
collects them to the List
.maxAll(Comparator, Collector)
,
maxAll()
public static <T> Collector<T,?,java.util.List<T>> maxAll(java.util.Comparator<? super T> comparator, int atMostSize)
comparator
- atMostSize
- public static <T extends java.lang.Comparable,A,D> Collector<T,?,D> maxAll(Collector<T,A,D> downstream)
Collector
which finds all the elements which are equal
to each other and bigger than any other element according to the natural
order. The found elements are reduced using the specified downstream
Collector
.T
- the type of the input elementsA
- the intermediate accumulation type of the downstream collectorD
- the result type of the downstream reductiondownstream
- a Collector
implementing the downstream
reductionCollector
which finds all the maximal elements.maxAll(Comparator, Collector)
,
maxAll(Comparator)
,
maxAll()
public static <T extends java.lang.Comparable> Collector<T,?,java.util.List<T>> maxAll()
Collector
which finds all the elements which are equal
to each other and bigger than any other element according to the natural
order. The found elements are collected to List
.T
- the type of the input elementsCollector
which finds all the maximal elements and
collects them to the List
.maxAll(Comparator)
,
maxAll(Collector)
public static <T,A,D> Collector<T,?,D> minAll(java.util.Comparator<? super T> comparator, Collector<T,A,D> downstream)
Collector
which finds all the elements which are equal
to each other and smaller than any other element according to the
specified Comparator
. The found elements are reduced using the
specified downstream Collector
.T
- the type of the input elementsA
- the intermediate accumulation type of the downstream collectorD
- the result type of the downstream reductioncomparator
- a Comparator
to compare the elementsdownstream
- a Collector
implementing the downstream
reductionCollector
which finds all the minimal elements.minAll(Comparator)
,
minAll(Collector)
,
minAll()
public static <T> Collector<T,?,java.util.List<T>> minAll(java.util.Comparator<? super T> comparator)
Collector
which finds all the elements which are equal
to each other and smaller than any other element according to the
specified Comparator
. The found elements are collected to
List
.T
- the type of the input elementscomparator
- a Comparator
to compare the elementsCollector
which finds all the minimal elements and
collects them to the List
.minAll(Comparator, Collector)
,
minAll()
public static <T> Collector<T,?,java.util.List<T>> minAll(java.util.Comparator<? super T> comparator, int atMostSize)
comparator
- atMostSize
- public static <T extends java.lang.Comparable,A,D> Collector<T,?,D> minAll(Collector<T,A,D> downstream)
Collector
which finds all the elements which are equal
to each other and smaller than any other element according to the natural
order. The found elements are reduced using the specified downstream
Collector
.T
- the type of the input elementsA
- the intermediate accumulation type of the downstream collectorD
- the result type of the downstream reductiondownstream
- a Collector
implementing the downstream
reductionCollector
which finds all the minimal elements.minAll(Comparator, Collector)
,
minAll(Comparator)
,
minAll()
public static <T extends java.lang.Comparable> Collector<T,?,java.util.List<T>> minAll()
Collector
which finds all the elements which are equal
to each other and smaller than any other element according to the natural
order. The found elements are collected to List
.T
- the type of the input elementsCollector
which finds all the minimal elements and
collects them to the List
.minAll(Comparator)
,
minAll(Collector)
public static <T> Collector<T,?,java.lang.Integer> summingInt(ToIntFunction<? super T> mapper)
Collector
that produces the sum of a integer-valued
function applied to the input elements. If no elements are present,
the result is 0.T
- the type of the input elementsmapper
- a function extracting the property to be summedCollector
that produces the sum of a derived propertypublic static <T> Collector<T,?,OptionalInt> summingInt2(ToIntFunction<? super T> mapper)
public static <T> Collector<T,?,java.lang.Long> summingLong(ToLongFunction<? super T> mapper)
Collector
that produces the sum of a long-valued
function applied to the input elements. If no elements are present,
the result is 0.T
- the type of the input elementsmapper
- a function extracting the property to be summedCollector
that produces the sum of a derived propertypublic static <T> Collector<T,?,OptionalLong> summingLong2(ToLongFunction<? super T> mapper)
public static <T> Collector<T,?,java.lang.Double> summingDouble(ToDoubleFunction<? super T> mapper)
Collector
that produces the sum of a double-valued
function applied to the input elements. If no elements are present,
the result is 0.
The sum returned can vary depending upon the order in which
values are recorded, due to accumulated rounding error in
addition of values of differing magnitudes. Values sorted by increasing
absolute magnitude tend to yield more accurate results. If any recorded
value is a NaN
or the sum is at any point a NaN
then the
sum will be NaN
.
T
- the type of the input elementsmapper
- a function extracting the property to be summedCollector
that produces the sum of a derived propertypublic static <T> Collector<T,?,OptionalDouble> summingDouble2(ToDoubleFunction<? super T> mapper)
public static <T> Collector<T,?,java.lang.Double> averagingInt(ToIntFunction<? super T> mapper)
Collector
that produces the arithmetic mean of an integer-valued
function applied to the input elements. If no elements are present,
the result is 0.T
- the type of the input elementsmapper
- a function extracting the property to be summedCollector
that produces the sum of a derived propertypublic static <T> Collector<T,?,OptionalDouble> averagingInt2(ToIntFunction<? super T> mapper)
public static <T> Collector<T,?,java.lang.Double> averagingLong(ToLongFunction<? super T> mapper)
Collector
that produces the arithmetic mean of a long-valued
function applied to the input elements. If no elements are present,
the result is 0.T
- the type of the input elementsmapper
- a function extracting the property to be summedCollector
that produces the sum of a derived propertypublic static <T> Collector<T,?,OptionalDouble> averagingLong2(ToLongFunction<? super T> mapper)
public static <T> Collector<T,?,java.lang.Double> averagingDouble(ToDoubleFunction<? super T> mapper)
Collector
that produces the arithmetic mean of a double-valued
function applied to the input elements. If no elements are present,
the result is 0.
The average returned can vary depending upon the order in which
values are recorded, due to accumulated rounding error in
addition of values of differing magnitudes. Values sorted by increasing
absolute magnitude tend to yield more accurate results. If any recorded
value is a NaN
or the sum is at any point a NaN
then the
average will be NaN
.
T
- the type of the input elementsmapper
- a function extracting the property to be summedCollector
that produces the sum of a derived propertypublic static <T> Collector<T,?,OptionalDouble> averagingDouble2(ToDoubleFunction<? super T> mapper)
public static <T> Collector<T,?,com.landawn.abacus.util.CharSummaryStatistics> summarizingChar(ToCharFunction<? super T> mapper)
public static <T> Collector<T,?,com.landawn.abacus.util.ByteSummaryStatistics> summarizingByte(ToByteFunction<? super T> mapper)
public static <T> Collector<T,?,com.landawn.abacus.util.ShortSummaryStatistics> summarizingShort(ToShortFunction<? super T> mapper)
public static <T> Collector<T,?,IntSummaryStatistics> summarizingInt(ToIntFunction<? super T> mapper)
Collector
which applies an int
-producing
mapping function to each input element, and returns summary statistics
for the resulting values.T
- the type of the input elementsmapper
- a mapping function to apply to each elementCollector
implementing the summary-statistics reductionsummarizingDouble(ToDoubleFunction)
,
summarizingLong(ToLongFunction)
public static <T> Collector<T,?,LongSummaryStatistics> summarizingLong(ToLongFunction<? super T> mapper)
Collector
which applies an long
-producing
mapping function to each input element, and returns summary statistics
for the resulting values.T
- the type of the input elementsmapper
- the mapping function to apply to each elementCollector
implementing the summary-statistics reductionsummarizingDouble(ToDoubleFunction)
,
summarizingInt(ToIntFunction)
public static <T> Collector<T,?,com.landawn.abacus.util.FloatSummaryStatistics> summarizingFloat(ToFloatFunction<? super T> mapper)
public static <T> Collector<T,?,DoubleSummaryStatistics> summarizingDouble(ToDoubleFunction<? super T> mapper)
Collector
which applies an double
-producing
mapping function to each input element, and returns summary statistics
for the resulting values.T
- the type of the input elementsmapper
- a mapping function to apply to each elementCollector
implementing the summary-statistics reductionsummarizingLong(ToLongFunction)
,
summarizingInt(ToIntFunction)
public static <T> Collector<T,?,T> reducing(T identity, BinaryOperator<T> op)
Collector
which performs a reduction of its
input elements under a specified BinaryOperator
using the
provided identity.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 operationreducing(BinaryOperator)
,
reducing(Object, Function, BinaryOperator)
public static <T> Collector<T,?,Nullable<T>> reducing(BinaryOperator<T> op)
Collector
which performs a reduction of its
input elements under a specified BinaryOperator
. The result
is described as an Nullable<T>
.T
- element type for the input and output of the reductionop
- a BinaryOperator<T>
used to reduce the input elementsCollector
which implements the reduction operationreducing(Object, BinaryOperator)
,
reducing(Object, Function, BinaryOperator)
public static <T> Collector<T,?,T> reducingOrGet(BinaryOperator<T> op, Supplier<? extends T> other)
public static <T,X extends java.lang.RuntimeException> Collector<T,?,T> reducingOrThrow(BinaryOperator<T> op, Supplier<? extends X> exceptionSupplier)
public static <T,U> Collector<T,?,U> reducing(U identity, Function<? super T,? extends U> mapper, BinaryOperator<U> op)
Collector
which performs a reduction of its
input elements under a specified mapping function and
BinaryOperator
. This is a generalization of
reducing(Object, BinaryOperator)
which allows a transformation
of the elements before reduction.T
- the type of the input elementsU
- 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 operationreducing(Object, BinaryOperator)
,
reducing(BinaryOperator)
public static <T,U> Collector<T,?,Nullable<U>> reducing(Function<? super T,? extends U> mapper, BinaryOperator<U> op)
public static <T,U> Collector<T,?,U> reducingOrGet(Function<? super T,? extends U> mapper, BinaryOperator<U> op, Supplier<? extends U> other)
public static <T,U,X extends java.lang.RuntimeException> Collector<T,?,U> reducingOrThrow(Function<? super T,? extends U> mapper, BinaryOperator<U> op, Supplier<? extends X> exceptionSupplier)
public static Collector<java.lang.CharSequence,?,java.lang.String> commonPrefix()
Collector
which computes a common prefix of input
CharSequence
objects returning the result as String
. For
empty input the empty String
is returned.
The returned Collector
handles specially Unicode surrogate pairs:
the returned prefix may end with
Unicode high-surrogate code unit only if it's not succeeded by
Unicode low-surrogate code unit in any of the input sequences.
Normally the ending high-surrogate code unit is removed from the prefix.
This method returns a short-circuiting collector: it may not process all the elements if the common prefix is empty.
Collector
which computes a common prefix.public static Collector<java.lang.CharSequence,?,java.lang.String> commonSuffix()
Collector
which computes a common suffix of input
CharSequence
objects returning the result as String
. For
empty input the empty String
is returned.
The returned Collector
handles specially Unicode surrogate pairs:
the returned suffix may start with
Unicode low-surrogate code unit only if it's not preceded by
Unicode high-surrogate code unit in any of the input sequences.
Normally the starting low-surrogate code unit is removed from the suffix.
This method returns a short-circuiting collector: it may not process all the elements if the common suffix is empty.
Collector
which computes a common suffix.public static <T> Collector<T,?,java.util.List<T>> dominators(BiPredicate<? super T,? super T> isDominator)
List
removing the elements following their dominator element. The dominator
elements are defined according to given isDominator BiPredicate
.
The isDominator relation must be transitive (if A dominates over B and B
dominates over C, then A also dominates over C).
This operation is similar to
streamEx.collapse(isDominator).toList()
. The important difference
is that in this method BiPredicate
accepts not the adjacent
stream elements, but the leftmost element of the series (current
dominator) and the current element.
For example, consider the stream of numbers:
StreamEx<Integer> stream = StreamEx.of(1, 5, 3, 4, 2, 7);
Using stream.collapse((a, b) -> a >= b).toList()
you will get the
numbers which are bigger than their immediate predecessor (
[1, 5, 4, 7]
), because (3, 4) pair is not collapsed. However
using stream.collect(dominators((a, b) -> a >= b))
you will get
the numbers which are bigger than any predecessor ([1, 5, 7]
) as
5 is the dominator element for the subsequent 3, 4 and 2.
T
- type of the input elements.isDominator
- a non-interfering, stateless, transitive
BiPredicate
which returns true if the first argument is
the dominator for the second argument.List
leaving only dominator elements.java.lang.UnsupportedOperationException
- it's used in parallel stream.public static <T,K> Collector<T,?,java.util.Map<K,java.util.List<T>>> groupingBy(Function<? super T,? extends K> classifier)
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
.
The classification function maps elements to some key type K
.
The collector produces a Map<K, List<T>>
whose keys are the
values resulting from applying the classification function to the input
elements, and whose corresponding values are List
s containing the
input elements which map to the associated key under the classification
function.
There are no guarantees on the type, mutability, serializability, or
thread-safety of the Map
or List
objects returned.
T
- the type of the input elementsK
- the type of the keysclassifier
- the classifier function mapping input elements to keysCollector
implementing the group-by operationgroupingBy(Function, Collector)
,
groupingBy(Function, Collector, Supplier)
,
groupingByConcurrent(Function)
public static <T,K,M extends java.util.Map<K,java.util.List<T>>> Collector<T,?,M> groupingBy(Function<? super T,? extends K> classifier, Supplier<M> mapFactory)
public static <T,K,A,D> Collector<T,?,java.util.Map<K,D>> groupingBy(Function<? super T,? extends K> classifier, Collector<? super T,A,D> downstream)
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
.
The classification function maps elements to some key type K
.
The downstream collector operates on elements of type T
and
produces a result of type D
. The resulting collector produces a
Map<K, D>
.
There are no guarantees on the type, mutability,
serializability, or thread-safety of the Map
returned.
For example, to compute the set of last names of people in each city:
Map<City, Set<String>> namesByCity
= people.stream().collect(groupingBy(Person::getCity,
mapping(Person::getLastName, toSet())));
T
- the type of the input elementsK
- the type of the keysA
- the intermediate accumulation type of the downstream collectorD
- the result type of the downstream reductionclassifier
- a classifier function mapping input elements to keysdownstream
- a Collector
implementing the downstream reductionCollector
implementing the cascaded group-by operationgroupingBy(Function)
,
groupingBy(Function, Collector, Supplier)
,
groupingByConcurrent(Function, Collector)
public static <T,K,A,D,M extends java.util.Map<K,D>> Collector<T,?,M> groupingBy(Function<? super T,? extends K> classifier, Collector<? super T,A,D> downstream, Supplier<M> mapFactory)
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
. The Map
produced by the Collector is created
with the supplied factory function.
The classification function maps elements to some key type K
.
The downstream collector operates on elements of type T
and
produces a result of type D
. The resulting collector produces a
Map<K, D>
.
For example, to compute the set of last names of people in each city, where the city names are sorted:
Map<City, Set<String>> namesByCity
= people.stream().collect(groupingBy(Person::getCity, TreeMap::new,
mapping(Person::getLastName, toSet())));
T
- the type of the input elementsK
- the type of the keysA
- the intermediate accumulation type of the downstream collectorD
- the result type of the downstream reductionM
- the type of the resulting Map
classifier
- a classifier function mapping input elements to keysdownstream
- a Collector
implementing the downstream reductionmapFactory
- a function which, when called, produces a new empty
Map
of the desired typeCollector
implementing the cascaded group-by operationgroupingBy(Function, Collector)
,
groupingBy(Function)
,
groupingByConcurrent(Function, Collector, Supplier)
public static <T,K> Collector<T,?,java.util.concurrent.ConcurrentMap<K,java.util.List<T>>> groupingByConcurrent(Function<? super T,? extends K> classifier)
Collector
implementing a "group by"
operation on input elements of type T
, grouping elements
according to a classification function.
This is a concurrent
and
unordered
Collector.
The classification function maps elements to some key type K
.
The collector produces a ConcurrentMap<K, List<T>>
whose keys are the
values resulting from applying the classification function to the input
elements, and whose corresponding values are List
s containing the
input elements which map to the associated key under the classification
function.
There are no guarantees on the type, mutability, or serializability
of the Map
or List
objects returned, or of the
thread-safety of the List
objects returned.
T
- the type of the input elementsK
- the type of the keysclassifier
- a classifier function mapping input elements to keysCollector
implementing the group-by operationgroupingBy(Function)
,
groupingByConcurrent(Function, Collector)
,
groupingByConcurrent(Function, Collector, Supplier)
public static <T,K,M extends java.util.concurrent.ConcurrentMap<K,java.util.List<T>>> Collector<T,?,M> groupingByConcurrent(Function<? super T,? extends K> classifier, Supplier<M> mapFactory)
public static <T,K,A,D> Collector<T,?,java.util.concurrent.ConcurrentMap<K,D>> groupingByConcurrent(Function<? super T,? extends K> classifier, Collector<? super T,A,D> downstream)
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
.
This is a concurrent
and
unordered
Collector.
The classification function maps elements to some key type K
.
The downstream collector operates on elements of type T
and
produces a result of type D
. The resulting collector produces a
Map<K, D>
.
For example, to compute the set of last names of people in each city, where the city names are sorted:
ConcurrentMap<City, Set<String>> namesByCity
= people.stream().collect(groupingByConcurrent(Person::getCity,
mapping(Person::getLastName, toSet())));
T
- the type of the input elementsK
- the type of the keysA
- the intermediate accumulation type of the downstream collectorD
- the result type of the downstream reductionclassifier
- a classifier function mapping input elements to keysdownstream
- a Collector
implementing the downstream reductionCollector
implementing the cascaded group-by operationgroupingBy(Function, Collector)
,
groupingByConcurrent(Function)
,
groupingByConcurrent(Function, Collector, Supplier)
public static <T,K,A,D,M extends java.util.concurrent.ConcurrentMap<K,D>> Collector<T,?,M> groupingByConcurrent(Function<? super T,? extends K> classifier, Collector<? super T,A,D> downstream, Supplier<M> mapFactory)
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
. The ConcurrentMap
produced by the
Collector is created with the supplied factory function.
This is a concurrent
and
unordered
Collector.
The classification function maps elements to some key type K
.
The downstream collector operates on elements of type T
and
produces a result of type D
. The resulting collector produces a
Map<K, D>
.
For example, to compute the set of last names of people in each city, where the city names are sorted:
ConcurrentMap<City, Set<String>> namesByCity
= people.stream().collect(groupingBy(Person::getCity, ConcurrentSkipListMap::new,
mapping(Person::getLastName, toSet())));
T
- the type of the input elementsK
- the type of the keysA
- the intermediate accumulation type of the downstream collectorD
- the result type of the downstream reductionM
- the type of the resulting ConcurrentMap
classifier
- a classifier function mapping input elements to keysdownstream
- a Collector
implementing the downstream reductionmapFactory
- a function which, when called, produces a new empty
ConcurrentMap
of the desired typeCollector
implementing the cascaded group-by operationgroupingByConcurrent(Function)
,
groupingByConcurrent(Function, Collector)
,
groupingBy(Function, Collector, Supplier)
public static <T> Collector<T,?,java.util.Map<java.lang.Boolean,java.util.List<T>>> partitioningBy(Predicate<? super T> predicate)
Collector
which partitions the input elements according
to a Predicate
, and organizes them into a
Map<Boolean, List<T>>
.
There are no guarantees on the type, mutability,
serializability, or thread-safety of the Map
returned.T
- the type of the input elementspredicate
- a predicate used for classifying input elementsCollector
implementing the partitioning operationpartitioningBy(Predicate, Collector)
public static <T,D,A> Collector<T,?,java.util.Map<java.lang.Boolean,D>> partitioningBy(Predicate<? super T> predicate, Collector<? super T,A,D> downstream)
Collector
which partitions the input elements according
to a Predicate
, reduces the values in each partition according to
another Collector
, and organizes them into a
Map<Boolean, D>
whose values are the result of the downstream
reduction.
There are no guarantees on the type, mutability,
serializability, or thread-safety of the Map
returned.
T
- the type of the input elementsA
- the intermediate accumulation type of the downstream collectorD
- the result type of the downstream reductionpredicate
- a predicate used for classifying input elementsdownstream
- a Collector
implementing the downstream
reductionCollector
implementing the cascaded partitioning
operationpartitioningBy(Predicate)
public static <K,V> Collector<java.util.Map.Entry<K,V>,?,java.util.Map<K,V>> toMap()
public static <K,V> Collector<java.util.Map.Entry<K,V>,?,java.util.Map<K,V>> toMap(BinaryOperator<V> mergeFunction)
public static <K,V,M extends java.util.Map<K,V>> Collector<java.util.Map.Entry<K,V>,?,M> toMap(Supplier<M> mapFactory)
public static <K,V,M extends java.util.Map<K,V>> Collector<java.util.Map.Entry<K,V>,?,M> toMap(BinaryOperator<V> mergeFunction, Supplier<M> mapFactory)
public static <T,K,U> Collector<T,?,java.util.Map<K,U>> toMap(Function<? super T,? extends K> keyExtractor, Function<? super T,? extends U> valueMapper)
Collector
that accumulates elements into a
Map
whose keys and values are the result of applying the provided
mapping functions to the input elements.
If the mapped keys contains duplicates (according to
Object.equals(Object)
), an IllegalStateException
is
thrown when the collection operation is performed. If the mapped keys
may have duplicates, use toMap(Function, Function, BinaryOperator)
instead.
T
- the type of the input elementsK
- the output type of the key mapping functionU
- the output type of the value mapping functionkeyExtractor
- a mapping function to produce keysvalueMapper
- a mapping function to produce valuesCollector
which collects elements into a Map
whose keys and values are the result of applying mapping functions to
the input elementstoMap(Function, Function, BinaryOperator)
,
toMap(Function, Function, BinaryOperator, Supplier)
,
toConcurrentMap(Function, Function)
public static <T,K,U> Collector<T,?,java.util.Map<K,U>> toMap(Function<? super T,? extends K> keyExtractor, Function<? super T,? extends U> valueMapper, BinaryOperator<U> mergeFunction)
Collector
that accumulates elements into a
Map
whose keys and values are the result of applying the provided
mapping functions to the input elements.
If the mapped
keys contains duplicates (according to Object.equals(Object)
),
the value mapping function is applied to each equal element, and the
results are merged using the provided merging function.
T
- the type of the input elementsK
- the output type of the key mapping functionU
- the output type of the value mapping functionkeyExtractor
- a mapping function to produce keysvalueMapper
- a mapping function to produce valuesmergeFunction
- a merge function, used to resolve collisions between
values associated with the same key, as supplied
to Map.merge(Object, Object, BiFunction)
Collector
which collects elements into a Map
whose keys are the result of applying a key mapping function to the input
elements, and whose values are the result of applying a value mapping
function to all input elements equal to the key and combining them
using the merge functiontoMap(Function, Function)
,
toMap(Function, Function, BinaryOperator, Supplier)
,
toConcurrentMap(Function, Function, BinaryOperator)
public static <T,K,U,M extends java.util.Map<K,U>> Collector<T,?,M> toMap(Function<? super T,? extends K> keyExtractor, Function<? super T,? extends U> valueMapper, Supplier<M> mapFactory)
public static <T,K,U,M extends java.util.Map<K,U>> Collector<T,?,M> toMap(Function<? super T,? extends K> keyExtractor, Function<? super T,? extends U> valueMapper, BinaryOperator<U> mergeFunction, Supplier<M> mapFactory)
Collector
that accumulates elements into a
Map
whose keys and values are the result of applying the provided
mapping functions to the input elements.
If the mapped
keys contains duplicates (according to Object.equals(Object)
),
the value mapping function is applied to each equal element, and the
results are merged using the provided merging function. The Map
is created by a provided supplier function.
T
- the type of the input elementsK
- the output type of the key mapping functionU
- the output type of the value mapping functionM
- the type of the resulting Map
keyExtractor
- a mapping function to produce keysvalueMapper
- a mapping function to produce valuesmergeFunction
- a merge function, used to resolve collisions between
values associated with the same key, as supplied
to Map.merge(Object, Object, BiFunction)
mapFactory
- a function which returns a new, empty Map
into
which the results will be insertedCollector
which collects elements into a Map
whose keys are the result of applying a key mapping function to the input
elements, and whose values are the result of applying a value mapping
function to all input elements equal to the key and combining them
using the merge functiontoMap(Function, Function)
,
toMap(Function, Function, BinaryOperator)
,
toConcurrentMap(Function, Function, BinaryOperator, Supplier)
public static <K,V> Collector<java.util.Map.Entry<K,V>,?,ImmutableMap<K,V>> toImmutableMap()
public static <K,V> Collector<java.util.Map.Entry<K,V>,?,ImmutableMap<K,V>> toImmutableMap(BinaryOperator<V> mergeFunction)
public static <T,K,U> Collector<T,?,ImmutableMap<K,U>> toImmutableMap(Function<? super T,? extends K> keyExtractor, Function<? super T,? extends U> valueMapper)
public static <T,K,U> Collector<T,?,ImmutableMap<K,U>> toImmutableMap(Function<? super T,? extends K> keyExtractor, Function<? super T,? extends U> valueMapper, BinaryOperator<U> mergeFunction)
public static <T,K,U> Collector<T,?,java.util.LinkedHashMap<K,U>> toLinkedHashMap(Function<? super T,? extends K> keyExtractor, Function<? super T,? extends U> valueMapper)
keyExtractor
- valueMapper
- toMap(Function, Function)
public static <T,K,U> Collector<T,?,java.util.LinkedHashMap<K,U>> toLinkedHashMap(Function<? super T,? extends K> keyExtractor, Function<? super T,? extends U> valueMapper, BinaryOperator<U> mergeFunction)
keyExtractor
- valueMapper
- mergeFunction
- toMap(Function, Function, BinaryOperator)
public static <T,K,U> Collector<T,?,java.util.concurrent.ConcurrentMap<K,U>> toConcurrentMap(Function<? super T,? extends K> keyExtractor, Function<? super T,? extends U> valueMapper)
Collector
that accumulates elements into a
ConcurrentMap
whose keys and values are the result of applying
the provided mapping functions to the input elements.
If the mapped keys contains duplicates (according to
Object.equals(Object)
), an IllegalStateException
is
thrown when the collection operation is performed. If the mapped keys
may have duplicates, use
toConcurrentMap(Function, Function, BinaryOperator)
instead.
T
- the type of the input elementsK
- the output type of the key mapping functionU
- the output type of the value mapping functionkeyExtractor
- the mapping function to produce keysvalueMapper
- the mapping function to produce valuesCollector
which collects elements into a
ConcurrentMap
whose keys are the result of applying a key mapping
function to the input elements, and whose values are the result of
applying a value mapping function to the input elementstoMap(Function, Function)
,
toConcurrentMap(Function, Function, BinaryOperator)
,
toConcurrentMap(Function, Function, BinaryOperator, Supplier)
public static <T,K,U,M extends java.util.concurrent.ConcurrentMap<K,U>> Collector<T,?,M> toConcurrentMap(Function<? super T,? extends K> keyExtractor, Function<? super T,? extends U> valueMapper, Supplier<M> mapFactory)
public static <T,K,U> Collector<T,?,java.util.concurrent.ConcurrentMap<K,U>> toConcurrentMap(Function<? super T,? extends K> keyExtractor, Function<? super T,? extends U> valueMapper, BinaryOperator<U> mergeFunction)
Collector
that accumulates elements into a
ConcurrentMap
whose keys and values are the result of applying
the provided mapping functions to the input elements.
If the mapped keys contains duplicates (according to Object.equals(Object)
),
the value mapping function is applied to each equal element, and the
results are merged using the provided merging function.
T
- the type of the input elementsK
- the output type of the key mapping functionU
- the output type of the value mapping functionkeyExtractor
- a mapping function to produce keysvalueMapper
- a mapping function to produce valuesmergeFunction
- a merge function, used to resolve collisions between
values associated with the same key, as supplied
to Map.merge(Object, Object, BiFunction)
Collector
which collects elements into a
ConcurrentMap
whose keys are the result of applying a key mapping
function to the input elements, and whose values are the result of
applying a value mapping function to all input elements equal to the key
and combining them using the merge functiontoConcurrentMap(Function, Function)
,
toConcurrentMap(Function, Function, BinaryOperator, Supplier)
,
toMap(Function, Function, BinaryOperator)
public static <T,K,U,M extends java.util.concurrent.ConcurrentMap<K,U>> Collector<T,?,M> toConcurrentMap(Function<? super T,? extends K> keyExtractor, Function<? super T,? extends U> valueMapper, BinaryOperator<U> mergeFunction, Supplier<M> mapFactory)
Collector
that accumulates elements into a
ConcurrentMap
whose keys and values are the result of applying
the provided mapping functions to the input elements.
If the mapped keys contains duplicates (according to Object.equals(Object)
),
the value mapping function is applied to each equal element, and the
results are merged using the provided merging function. The
ConcurrentMap
is created by a provided supplier function.
This is a concurrent
and
unordered
Collector.
T
- the type of the input elementsK
- the output type of the key mapping functionU
- the output type of the value mapping functionM
- the type of the resulting ConcurrentMap
keyExtractor
- a mapping function to produce keysvalueMapper
- a mapping function to produce valuesmergeFunction
- a merge function, used to resolve collisions between
values associated with the same key, as supplied
to Map.merge(Object, Object, BiFunction)
mapFactory
- a function which returns a new, empty Map
into
which the results will be insertedCollector
which collects elements into a
ConcurrentMap
whose keys are the result of applying a key mapping
function to the input elements, and whose values are the result of
applying a value mapping function to all input elements equal to the key
and combining them using the merge functiontoConcurrentMap(Function, Function)
,
toConcurrentMap(Function, Function, BinaryOperator)
,
toMap(Function, Function, BinaryOperator, Supplier)
public static <T,K,U> Collector<T,?,BiMap<K,U>> toBiMap(Function<? super T,? extends K> keyExtractor, Function<? super T,? extends U> valueMapper)
public static <T,K,U> Collector<T,?,BiMap<K,U>> toBiMap(Function<? super T,? extends K> keyExtractor, Function<? super T,? extends U> valueMapper, Supplier<BiMap<K,U>> mapFactory)
public static <T,K,U> Collector<T,?,BiMap<K,U>> toBiMap(Function<? super T,? extends K> keyExtractor, Function<? super T,? extends U> valueMapper, BinaryOperator<U> mergeFunction)
public static <T,K,U> Collector<T,?,BiMap<K,U>> toBiMap(Function<? super T,? extends K> keyExtractor, Function<? super T,? extends U> valueMapper, BinaryOperator<U> mergeFunction, Supplier<BiMap<K,U>> mapFactory)
public static <K,E> Collector<java.util.Map.Entry<? extends K,? extends E>,?,ListMultimap<K,E>> toMultimap()
public static <K,E,V extends java.util.Collection<E>,M extends Multimap<K,E,V>> Collector<java.util.Map.Entry<? extends K,? extends E>,?,M> toMultimap(Supplier<M> mapFactory)
public static <T,K> Collector<T,?,ListMultimap<K,T>> toMultimap(Function<? super T,? extends K> keyExtractor)
public static <T,K,V extends java.util.Collection<T>,M extends Multimap<K,T,V>> Collector<T,?,M> toMultimap(Function<? super T,? extends K> keyExtractor, Supplier<M> mapFactory)
public static <T,K,U> Collector<T,?,ListMultimap<K,U>> toMultimap(Function<? super T,? extends K> keyExtractor, Function<? super T,? extends U> valueMapper)
public static <T,K,U,V extends java.util.Collection<U>,M extends Multimap<K,U,V>> Collector<T,?,M> toMultimap(Function<? super T,? extends K> keyExtractor, Function<? super T,? extends U> valueMapper, Supplier<M> mapFactory)