public final class MoreCollectors extends Object
Collectors,
Joining| Modifier and Type | Method and Description |
|---|---|
static <T> Collector<T,?,OptionalInt> |
andingInt(ToIntFunction<T> mapper)
Returns a
Collector which performs the bitwise-and operation of a
integer-valued function applied to the input elements. |
static <T> Collector<T,?,OptionalLong> |
andingLong(ToLongFunction<T> mapper)
Returns a
Collector which performs the bitwise-and operation of a
long-valued function applied to the input elements. |
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<CharSequence,?,String> |
commonPrefix()
Returns a
Collector which computes a common prefix of input
CharSequence objects returning the result as String. |
static Collector<CharSequence,?,String> |
commonSuffix()
Returns a
Collector which computes a common suffix of input
CharSequence objects returning the result as String. |
static <T> Collector<T,?,Integer> |
countingInt()
Returns a
Collector accepting elements of type T that
counts the number of input elements and returns result as Integer
. |
static <T> Collector<T,?,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,?,Integer> |
distinctCount(Function<? super T,?> mapper)
Returns a
Collector which counts a number of distinct values the
mapper function returns for the stream elements. |
static <T> Collector<T,?,List<T>> |
dominators(BiPredicate<? super T,? super T> isDominator)
Returns a collector which collects input elements into
List
removing the elements following their dominator element. |
static <T> Collector<T,?,List<T>> |
filtering(Predicate<? super T> predicate)
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<T,A,R> downstream)
Returns a
Collector which passes only those elements to the
specified downstream collector which match given predicate. |
static <T> Collector<T,?,Optional<T>> |
first()
Returns a
Collector which collects only the first stream element
if any. |
static <T,U> Collector<T,?,List<U>> |
flatMapping(Function<? super T,? extends Stream<? extends U>> mapper)
Returns a collector which launches a flat mapping function for each input
element and collects the elements of the resulting streams to the flat
List. |
static <T,U,A,R> Collector<T,?,R> |
flatMapping(Function<? super T,? extends Stream<? 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 flat mapping function
to each input element before accumulation. |
static <T> Collector<T,?,List<T>> |
greatest(Comparator<? super T> comparator,
int n)
Returns a
Collector which collects at most specified number of
the greatest stream elements according to the specified
Comparator into the List. |
static <T extends Comparable<? super T>> |
greatest(int n)
Returns a
Collector which collects at most specified number of
the greatest stream elements according to the natural order into the
List. |
static <T,K,D,A> Collector<T,?,Map<K,D>> |
groupingBy(Function<? super T,? extends K> classifier,
Set<K> domain,
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,D,A,M extends Map<K,D>> |
groupingBy(Function<? super T,? extends K> classifier,
Set<K> domain,
Supplier<M> mapFactory,
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 extends Enum<K>,A,D> |
groupingByEnum(Class<K> enumClass,
Function<? super T,K> classifier,
Collector<? super T,A,D> downstream)
Returns a
Collector implementing a cascaded "group by" operation
on input elements of type T, for classification function which
maps input elements to the enum values. |
static <T> Collector<T,?,List<T>> |
head(int n)
Returns a
Collector which collects at most specified number of
the first stream elements into the List. |
static <T,A,R> Collector<T,?,Optional<R>> |
ifAllMatch(Predicate<T> predicate,
Collector<T,A,R> downstream)
Returns a
Collector which performs downstream reduction if all
elements satisfy the Predicate. |
static <T,S extends Collection<T>> |
intersecting()
Returns a
Collector which collects the intersection of the input
collections into the newly-created Set. |
static <T> Collector<T,?,Optional<T>> |
last()
Returns a
Collector which collects only the last stream element
if any. |
static <T> Collector<T,?,List<T>> |
least(Comparator<? super T> comparator,
int n)
Returns a
Collector which collects at most specified number of
the least stream elements according to the specified Comparator
into the List. |
static <T extends Comparable<? super T>> |
least(int n)
Returns a
Collector which collects at most specified number of
the least stream elements according to the natural order into the
List. |
static <T,U> Collector<T,?,List<U>> |
mapping(Function<? super T,? extends U> mapper)
Returns a collector which collects input elements to the new
List
transforming them with the supplied function beforehand. |
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 Comparable<? super T>> |
maxAll()
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 Comparable<? super T>,A,D> |
maxAll(Collector<T,A,D> downstream)
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,?,List<T>> |
maxAll(Comparator<? super T> comparator)
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(Comparator<? super T> comparator,
Collector<? super T,A,D> downstream)
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 extends Comparable<? super T>> |
maxIndex()
Returns a
Collector which finds the index of the maximal stream
element according to the elements natural order. |
static <T> Collector<T,?,OptionalLong> |
maxIndex(Comparator<? super T> comparator)
Returns a
Collector which finds the index of the maximal stream
element according to the specified Comparator. |
static <T extends Comparable<? super T>> |
minAll()
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 Comparable<? super T>,A,D> |
minAll(Collector<T,A,D> downstream)
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,?,List<T>> |
minAll(Comparator<? super T> comparator)
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(Comparator<? super T> comparator,
Collector<T,A,D> downstream)
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 extends Comparable<? super T>> |
minIndex()
Returns a
Collector which finds the index of the minimal stream
element according to the elements natural order. |
static <T> Collector<T,?,OptionalLong> |
minIndex(Comparator<? super T> comparator)
Returns a
Collector which finds the index of the minimal stream
element according to the specified Comparator. |
static <T,R> Collector<T,?,Optional<R>> |
minMax(Comparator<? super T> comparator,
BiFunction<? super T,? super T,? extends R> finisher)
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> Collector<T,?,Optional<T>> |
onlyOne()
Returns a
Collector which collects the stream element if stream
contains exactly one element. |
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)
Returns a
Collector which aggregates the results of two supplied
collectors using the supplied finisher function. |
static <T,D,A> Collector<T,?,Map<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,?,List<T>> |
tail(int n)
Returns a
Collector which collects at most specified number of
the last stream elements into the List. |
static <T> Collector<T,?,T[]> |
toArray(IntFunction<T[]> generator)
Returns a
Collector that accumulates the input elements into a
new array. |
static <T> Collector<T,?,boolean[]> |
toBooleanArray(Predicate<T> predicate)
Returns a
Collector which produces a boolean array containing the
results of applying the given predicate to the input elements, in
encounter order. |
static <T extends Enum<T>> |
toEnumSet(Class<T> enumClass)
Returns a
Collector that accumulates the input enum values into a
new EnumSet. |
public static <T> Collector<T,?,T[]> toArray(IntFunction<T[]> generator)
Collector that accumulates the input elements into a
new array.
The operation performed by the returned collector is equivalent to
stream.toArray(generator). This collector is mostly useful as a
downstream collector.T - the type of the input elementsgenerator - a function which produces a new array of the desired
type and the provided lengthCollector which collects all the input elements into an
array, in encounter orderpublic static <T> Collector<T,?,boolean[]> toBooleanArray(Predicate<T> predicate)
Collector which produces a boolean array containing the
results of applying the given predicate to the input elements, in
encounter order.T - the type of the input elementspredicate - a non-interfering, stateless predicate to apply to each
input element. The result values of this predicate are collected
to the resulting boolean array.Collector which collects the results of the predicate
function to the boolean array, in encounter order.public static <T extends Enum<T>> Collector<T,?,EnumSet<T>> toEnumSet(Class<T> enumClass)
Collector that accumulates the input enum values into a
new EnumSet.
This method returns a short-circuiting collector: it may not process all the elements if the resulting set contains all possible enum values.
T - the type of the input elementsenumClass - the class of input enum valuesCollector which collects all the input elements into a
EnumSetpublic static <T> Collector<T,?,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,?,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,?,Integer> countingInt()
Collector accepting elements of type T that
counts the number of input elements and returns result as Integer
. If no elements are present, the result is 0.T - the type of the input elementsCollector that counts the input elementsCollectors.counting()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,?,Optional<R>> minMax(Comparator<? super T> comparator, BiFunction<? super T,? super 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 Optionalcomparator - 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(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,?,List<T>> maxAll(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 extends Comparable<? super T>,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 Comparable<? super T>> Collector<T,?,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(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,?,List<T>> minAll(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 extends Comparable<? super T>,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 Comparable<? super T>> Collector<T,?,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,?,Optional<T>> onlyOne()
Collector which collects the stream element if stream
contains exactly one element.
This method returns a short-circuiting collector.
T - the type of the input elementsOptional describing the only
element of the stream. For empty stream or stream containing more
than one element an empty Optional is returned.public static <T> Collector<T,?,Optional<T>> first()
Collector which collects only the first stream element
if any.
This method returns a short-circuiting collector.
The operation performed by the returned collector is equivalent to
stream.findFirst(). This collector is mostly useful as a
downstream collector.
T - the type of the input elementsOptional which describes the
first element of the stream. For empty stream an empty
Optional is returned.public static <T> Collector<T,?,Optional<T>> last()
Collector which collects only the last stream element
if any.T - the type of the input elementsOptional which describes the
last element of the stream. For empty stream an empty
Optional is returned.public static <T> Collector<T,?,List<T>> head(int n)
Collector which collects at most specified number of
the first stream elements into the List.
This method returns a short-circuiting collector.
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.limit(n).collect(Collectors.toList()). This collector is
mostly useful as a downstream collector.
T - the type of the input elementsn - maximum number of stream elements to preserveList containing the first n
stream elements or less if the stream was shorter.public static <T> Collector<T,?,List<T>> tail(int n)
Collector which collects at most specified number of
the last stream elements into the List.
There are no guarantees on the type, mutability, serializability, or
thread-safety of the List returned.
When supplied n is less or equal to zero, this method returns a
short-circuiting
collector which ignores the input and produces an empty list.
T - the type of the input elementsn - maximum number of stream elements to preserveList containing the last n
stream elements or less if the stream was shorter.public static <T> Collector<T,?,List<T>> greatest(Comparator<? super T> comparator, int n)
Collector which collects at most specified number of
the greatest stream elements according to the specified
Comparator into the List. The resulting List is
sorted in comparator reverse order (greatest element is the first). The
order of equal elements is the same as in the input stream.
The operation performed by the returned collector is equivalent to
stream.sorted(comparator.reversed()).limit(n).collect(Collectors.toList())
, but usually performed much faster if n is much less than the
stream size.
There are no guarantees on the type, mutability, serializability, or
thread-safety of the List returned.
When supplied n is less or equal to zero, this method returns a
short-circuiting
collector which ignores the input and produces an empty list.
T - the type of the input elementscomparator - the comparator to compare the elements byn - maximum number of stream elements to preserveList containing the greatest
n stream elements or less if the stream was shorter.public static <T extends Comparable<? super T>> Collector<T,?,List<T>> greatest(int n)
Collector which collects at most specified number of
the greatest stream elements according to the natural order into the
List. The resulting List is sorted in reverse order
(greatest element is the first). The order of equal elements is the same
as in the input stream.
The operation performed by the returned collector is equivalent to
stream.sorted(Comparator.reverseOrder()).limit(n).collect(Collectors.toList())
, but usually performed much faster if n is much less than the
stream size.
There are no guarantees on the type, mutability, serializability, or
thread-safety of the List returned.
When supplied n is less or equal to zero, this method returns a
short-circuiting
collector which ignores the input and produces an empty list.
T - the type of the input elementsn - maximum number of stream elements to preserveList containing the greatest
n stream elements or less if the stream was shorter.public static <T> Collector<T,?,List<T>> least(Comparator<? super T> comparator, int n)
Collector which collects at most specified number of
the least stream elements according to the specified Comparator
into the List. The resulting List is sorted in comparator
order (least element is the first). The order of equal elements is the
same as in the input stream.
The operation performed by the returned collector is equivalent to
stream.sorted(comparator).limit(n).collect(Collectors.toList()),
but usually performed much faster if n is much less than the
stream size.
There are no guarantees on the type, mutability, serializability, or
thread-safety of the List returned.
When supplied n is less or equal to zero, this method returns a
short-circuiting
collector which ignores the input and produces an empty list.
T - the type of the input elementscomparator - the comparator to compare the elements byn - maximum number of stream elements to preserveList containing the least n
stream elements or less if the stream was shorter.public static <T extends Comparable<? super T>> Collector<T,?,List<T>> least(int n)
Collector which collects at most specified number of
the least stream elements according to the natural order into the
List. The resulting List is sorted in natural order
(least element is the first). The order of equal elements is the same as
in the input stream.
The operation performed by the returned collector is equivalent to
stream.sorted().limit(n).collect(Collectors.toList()), but
usually performed much faster if n is much less than the stream
size.
There are no guarantees on the type, mutability, serializability, or
thread-safety of the List returned.
When supplied n is less or equal to zero, this method returns a
short-circuiting
collector which ignores the input and produces an empty list.
T - the type of the input elementsn - maximum number of stream elements to preserveList containing the least n
stream elements or less if the stream was shorter.public static <T> Collector<T,?,OptionalLong> minIndex(Comparator<? super T> comparator)
Collector which finds the index of the minimal stream
element according to the specified Comparator. If there are
several minimal elements, the index of the first one is returned.T - the type of the input elementscomparator - a Comparator to compare the elementsCollector which finds the index of the minimal element.minIndex()public static <T extends Comparable<? super T>> Collector<T,?,OptionalLong> minIndex()
Collector which finds the index of the minimal stream
element according to the elements natural order. If there are several
minimal elements, the index of the first one is returned.T - the type of the input elementsCollector which finds the index of the minimal element.minIndex(Comparator)public static <T> Collector<T,?,OptionalLong> maxIndex(Comparator<? super T> comparator)
Collector which finds the index of the maximal stream
element according to the specified Comparator. If there are
several maximal elements, the index of the first one is returned.T - the type of the input elementscomparator - a Comparator to compare the elementsCollector which finds the index of the maximal element.maxIndex()public static <T extends Comparable<? super T>> Collector<T,?,OptionalLong> maxIndex()
Collector which finds the index of the maximal stream
element according to the elements natural order. If there are several
maximal elements, the index of the first one is returned.T - the type of the input elementsCollector which finds the index of the maximal element.maxIndex(Comparator)public static <T,K extends Enum<K>,A,D> Collector<T,?,EnumMap<K,D>> groupingByEnum(Class<K> enumClass, Function<? super T,K> classifier, Collector<? super T,A,D> downstream)
Collector implementing a cascaded "group by" operation
on input elements of type T, for classification function which
maps input elements to the enum values. The downstream reduction for
repeating keys is performed using the specified downstream
Collector.
Unlike the Collectors.groupingBy(Function, Collector) collector
this collector produces an EnumMap which contains all possible
keys including keys which were never returned by the classification
function. These keys are mapped to the default collector value which is
equivalent to collecting an empty stream with the same collector.
This method returns a short-circuiting collector if the downstream collector is short-circuiting. The collection might stop when for every possible enum key the downstream collection is known to be finished.
T - the type of the input elementsK - the type of the enum values returned by the classifierA - the intermediate accumulation type of the downstream collectorD - the result type of the downstream reductionenumClass - the class of enum values returned by the classifierclassifier - a classifier function mapping input elements to enum
valuesdownstream - a Collector implementing the downstream
reductionCollector implementing the cascaded group-by operationCollectors.groupingBy(Function, Collector),
groupingBy(Function, Set, Supplier, Collector)public static <T,K,D,A> Collector<T,?,Map<K,D>> groupingBy(Function<? super T,? extends K> classifier, Set<K> domain, 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.
There are no guarantees on the type, mutability, serializability, or
thread-safety of the Map returned.
The main difference of this collector from
Collectors.groupingBy(Function, Collector) is that it accepts
additional domain parameter which is the Set of all possible map
keys. If the mapper function produces the key out of domain, an
IllegalStateException will occur. If the mapper function does not
produce some of domain keys at all, they are also added to the result.
These keys are mapped to the default collector value which is equivalent
to collecting an empty stream with the same collector.
This method returns a short-circuiting collector if the downstream collector is short-circuiting. The collection might stop when for every possible key from the domain the downstream collection is known to be finished.
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 keysdomain - a domain of all possible key valuesdownstream - a Collector implementing the downstream
reductionCollector implementing the cascaded group-by operation
with given domaingroupingBy(Function, Set, Supplier, Collector),
groupingByEnum(Class, Function, Collector)public static <T,K,D,A,M extends Map<K,D>> Collector<T,?,M> groupingBy(Function<? super T,? extends K> classifier, Set<K> domain, Supplier<M> mapFactory, 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 Map produced by the Collector is created
with the supplied factory function.
The main difference of this collector from
Collectors.groupingBy(Function, Supplier, Collector) is that it
accepts additional domain parameter which is the Set of all
possible map keys. If the mapper function produces the key out of domain,
an IllegalStateException will occur. If the mapper function does
not produce some of domain keys at all, they are also added to the
result. These keys are mapped to the default collector value which is
equivalent to collecting an empty stream with the same collector.
This method returns a short-circuiting collector if the downstream collector is short-circuiting. The collection might stop when for every possible key from the domain the downstream collection is known to be finished.
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 Mapclassifier - a classifier function mapping input elements to keysdomain - a domain of all possible key valuesdownstream - 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 operation
with given domaingroupingBy(Function, Set, Collector),
groupingByEnum(Class, Function, Collector)public static <T,S extends Collection<T>> Collector<S,?,Set<T>> intersecting()
Collector which collects the intersection of the input
collections into the newly-created Set.
The returned collector produces an empty set if the input is empty or intersection of the input collections is empty.
There are no guarantees on the type, mutability, serializability, or
thread-safety of the Set returned.
This method returns a short-circuiting collector: it may not process all the elements if the resulting intersection is empty.
T - the type of the elements in the input collectionsS - the type of the input collectionsCollector which finds all the minimal elements and
collects them to the List.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.
Unlike Collectors.collectingAndThen(Collector, Function) this
method returns a
short-circuiting
collector if the downstream collector is short-circuiting.
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 collectorCollectors.collectingAndThen(Collector, Function)public static <T,D,A> Collector<T,?,Map<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.
Unlike Collectors.partitioningBy(Predicate, Collector) this
method returns a
short-circuiting
collector if the downstream collector is short-circuiting.
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
operationCollectors.partitioningBy(Predicate, Collector)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.
Unlike Collectors.mapping(Function, Collector) this method
returns a
short-circuiting
collector if the downstream collector is short-circuiting.
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 valuesCollectors.mapping(Function, Collector)public static <T,U> Collector<T,?,List<U>> mapping(Function<? super T,? extends U> mapper)
List
transforming them with the supplied function beforehand.
This method behaves like
Collectors.mapping(mapper, Collectors.toList()).
There are no guarantees on the type, mutability, serializability, or
thread-safety of the List returned.
T - the type of the input elementsU - the resulting type of the mapper functionmapper - a function to be applied to the input elementsListmapping(Function, Collector)public static <T,U,A,R> Collector<T,?,R> flatMapping(Function<? super T,? extends Stream<? 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 flat mapping function
to each input element before accumulation. The flat mapping function maps
an input element to a stream covering zero or more output
elements that are then accumulated downstream. Each mapped stream is
closed after its contents
have been placed downstream. (If a mapped stream is null an empty
stream is used, instead.)
This method is similar to Collectors.flatMapping 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 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 elements, which
returns a stream of resultsdownstream - a collector which will receive the elements of the
stream returned by mapperpublic static <T,U> Collector<T,?,List<U>> flatMapping(Function<? super T,? extends Stream<? extends U>> mapper)
List. Each mapped stream is
closed after its contents
have been placed downstream. (If a mapped stream is null an empty
stream is used, instead.)
This method behaves like flatMapping(mapper, Collectors.toList())
.
There are no guarantees on the type, mutability, serializability, or
thread-safety of the List returned.
T - the type of the input elementsU - type of the resulting elementsmapper - a function to be applied to the input elements, which
returns a stream of resultsListpublic static <T,A,R> Collector<T,?,R> filtering(Predicate<? super T> predicate, Collector<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> Collector<T,?,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 elementsListfiltering(Predicate, Collector)public static <T> Collector<T,?,OptionalInt> andingInt(ToIntFunction<T> mapper)
Collector which performs the bitwise-and operation of a
integer-valued function applied to the input elements. If no elements are
present, the result is empty OptionalInt.
This method returns a short-circuiting collector: it may not process all the elements if the result is zero.
T - the type of the input elementsmapper - a function extracting the property to be processedCollector that produces the bitwise-and operation of a
derived propertypublic static <T> Collector<T,?,OptionalLong> andingLong(ToLongFunction<T> mapper)
Collector which performs the bitwise-and operation of a
long-valued function applied to the input elements. If no elements are
present, the result is empty OptionalLong.
This method returns a short-circuiting collector: it may not process all the elements if the result is zero.
T - the type of the input elementsmapper - a function extracting the property to be processedCollector that produces the bitwise-and operation of a
derived propertypublic static Collector<CharSequence,?,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<CharSequence,?,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,?,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.StreamEx.collapse(BiPredicate)public static <T,A,R> Collector<T,?,Optional<R>> ifAllMatch(Predicate<T> predicate, Collector<T,A,R> downstream)
Collector which performs downstream reduction if all
elements satisfy the Predicate. The result is described as an
Optional<R>.
The resulting collector returns an empty optional if at least one input element does not satisfy the predicate. Otherwise it returns an optional which contains the result of the downstream collector.
This method returns a short-circuiting collector: it may not process all the elements if some of items don't satisfy the predicate or if downstream collector is a short-circuiting collector.
It's guaranteed that the downstream collector is not called for elements which don't satisfy the predicate.
T - the type of input elementsA - intermediate accumulation type of the downstream collectorR - result type of the downstream collectorpredicate - a non-interfering, stateless predicate to checks whether
collector should proceed with elementdownstream - a Collector implementing the downstream
reductionCollector witch performs downstream reduction if all
elements satisfy the predicateStream.allMatch(Predicate),
AbstractStreamEx.dropWhile(Predicate),
AbstractStreamEx.takeWhile(Predicate)Copyright © 2017. All rights reserved.