K
- the key typeV
- the value typepublic final class MapStream<K,V> extends Object implements Stream<Map.Entry<K,V>>
Stream
wrapper that stream over Key-Value pairs. With this
wrapper you get access to additional operators for working with two valued
collections.Stream.Builder<T>
Modifier and Type | Method and Description |
---|---|
boolean |
allMatch(BiPredicate<? super K,? super V> predicate)
Returns whether all key-value pairs of this stream match the provided
predicate.
|
boolean |
allMatch(Predicate<? super Map.Entry<K,V>> predicate)
Returns whether all elements of this stream match the provided predicate.
|
boolean |
anyMatch(BiPredicate<? super K,? super V> predicate)
Returns whether any key-value pairs of this stream match the provided
predicate.
|
boolean |
anyMatch(Predicate<? super Map.Entry<K,V>> predicate)
Returns whether any elements of this stream match the provided
predicate.
|
void |
close()
Closes this stream, causing all close handlers for this stream pipeline
to be called.
|
<R,A> R |
collect(Collector<? super Map.Entry<K,V>,A,R> collector)
Performs a mutable reduction operation on the elements of this stream
using a
Collector . |
<R> R |
collect(Supplier<R> supplier,
BiConsumer<R,? super Map.Entry<K,V>> accumulator,
BiConsumer<R,R> combiner)
Performs a mutable reduction operation on the elements of this stream.
|
static <K> Comparator<K> |
comparing(Function<K,? extends Comparable<?>>... methods)
Utility method for creating a composed comparator that begins by
comparing the first value and if it is equal, continues to the next one.
|
long |
count()
Returns the count of elements in this stream.
|
MapStream<K,V> |
distinct()
Returns a stream consisting of the distinct elements (according to
Object.equals(Object) ) of this stream. |
MapStream<K,V> |
distinctKeys()
Returns a stream consisting of the distinct elements (according to
Object.equals(Object) ) of this stream based on the key only. |
MapStream<K,V> |
distinctKeys(BinaryOperator<V> merger)
Returns a stream consisting of the distinct elements (according to
Object.equals(Object) ) of this stream based on the key only. |
MapStream<K,V> |
distinctValues()
Returns a stream consisting of the distinct elements (according to
Object.equals(Object) ) of this stream based on the value only. |
MapStream<K,V> |
distinctValues(BinaryOperator<K> merger)
Returns a stream consisting of the distinct elements (according to
Object.equals(Object) ) of this stream based on the value only. |
static <K,V> MapStream<K,V> |
empty()
Constructs an empty
MapStream . |
MapStream<K,V> |
filter(BiPredicate<? super K,? super V> predicate)
Returns a stream consisting of the elements of this stream that match
the given predicate.
|
MapStream<K,V> |
filter(Predicate<? super Map.Entry<K,V>> predicate)
Returns a stream consisting of the elements of this stream that match
the given predicate.
|
MapStream<K,V> |
filterKey(Predicate<? super K> predicate)
Returns a stream consisting of the elements of this stream where the key
component matches the given predicate.
|
MapStream<K,V> |
filterValue(Predicate<? super V> predicate)
Returns a stream consisting of the elements of this stream where the
value component matches the given predicate.
|
Optional<Map.Entry<K,V>> |
findAny()
Returns an
Optional describing some element of the stream, or an
empty Optional if the stream is empty. |
Optional<Map.Entry<K,V>> |
findFirst()
Returns an
Optional describing the first element of this stream,
or an empty Optional if the stream is empty. |
<R> Stream<R> |
flatMap(BiFunction<? super K,? super V,? extends Stream<? extends R>> mapper)
Returns a stream consisting of the results of replacing each element of
this stream with the contents of a mapped stream produced by applying
the provided mapping function to each element.
|
<R> Stream<R> |
flatMap(Function<? super Map.Entry<K,V>,? extends Stream<? extends R>> mapper)
Returns a stream consisting of the results of replacing each element of
this stream with the contents of a mapped stream produced by applying
the provided mapping function to each element.
|
<R> MapStream<R,V> |
flatMapKey(BiFunction<? super K,? super V,? extends Stream<? extends R>> mapper)
Returns a stream where the key-component of this stream has been replaced
by the result of applying the specified mapping function to the entry.
|
<R> MapStream<R,V> |
flatMapKey(Function<? super K,? extends Stream<? extends R>> mapper)
Returns a stream where the key-component of this stream has been replaced
by the result of applying the specified mapping function to the entry.
|
DoubleStream |
flatMapToDouble(BiFunction<? super K,? super V,? extends DoubleStream> mapper)
Returns an
DoubleStream consisting of the results of replacing
each key-value pair of this stream with the contents of a mapped stream
produced by applying the provided mapping function to each element. |
DoubleStream |
flatMapToDouble(Function<? super Map.Entry<K,V>,? extends DoubleStream> mapper)
Returns an
DoubleStream consisting of the results of replacing
each element of this stream with the contents of a mapped stream produced
by applying the provided mapping function to each element. |
IntStream |
flatMapToInt(BiFunction<? super K,? super V,? extends IntStream> mapper)
Returns an
IntStream consisting of the results of replacing each
key-value pair of this stream with the contents of a mapped stream
produced by applying the provided mapping function to each element. |
IntStream |
flatMapToInt(Function<? super Map.Entry<K,V>,? extends IntStream> mapper)
Returns an
IntStream consisting of the results of replacing each
element of this stream with the contents of a mapped stream produced by
applying the provided mapping function to each element. |
LongStream |
flatMapToLong(BiFunction<? super K,? super V,? extends LongStream> mapper)
Returns an
LongStream consisting of the results of replacing each
key-value pair of this stream with the contents of a mapped stream
produced by applying the provided mapping function to each element. |
LongStream |
flatMapToLong(Function<? super Map.Entry<K,V>,? extends LongStream> mapper)
Returns an
LongStream consisting of the results of replacing each
element of this stream with the contents of a mapped stream produced by
applying the provided mapping function to each element. |
<R> MapStream<K,R> |
flatMapValue(BiFunction<? super K,? super V,? extends Stream<? extends R>> mapper)
Returns a stream where the value-component of this stream has been
replaced by the result of applying the specified mapping function to the
entry.
|
<R> MapStream<K,R> |
flatMapValue(Function<? super V,? extends Stream<? extends R>> mapper)
Returns a stream where the value-component of this stream has been
replaced by the result of applying the specified mapping function to the
entry.
|
static <K,V> MapStream<V,K> |
flip(MapStream<K,V> original)
Produces a new MapStream by flipping the specified one so that keys
become values and values become keys.
|
void |
forEach(BiConsumer<? super K,? super V> action)
Performs an action for each key-value pair of this stream.
|
void |
forEach(Consumer<? super Map.Entry<K,V>> action)
Performs an action for each element of this stream.
|
void |
forEachOrdered(BiConsumer<? super K,? super V> action)
Performs an action for each key-value pair of this stream, in the
encounter order of the stream if the stream has a defined encounter
order.
|
void |
forEachOrdered(Consumer<? super Map.Entry<K,V>> action)
Performs an action for each element of this stream, in the encounter
order of the stream if the stream has a defined encounter order.
|
static <K,V> MapStream<K,V> |
fromKeys(Stream<K> keys,
Function<K,V> valueFromKey)
Constructs a new
MapStream by wrapping a key-stream and
calculating the corresponding values using the supplied function. |
static <E,K,V> MapStream<K,V> |
fromStream(Stream<E> stream,
Function<E,K> keyMapper,
Function<E,V> valueMapper)
Constructs a new
MapStream by wrapping a stream of objects and
calculating both keys and values using the supplied functions. |
static <K,V> MapStream<K,V> |
fromValues(Stream<V> values,
Function<V,K> keyFromValue)
Constructs a new
MapStream by wrapping a value-stream and
calculating the corresponding keys using the supplied function. |
<K2> MapStream<K2,List<V>> |
groupingBy(Function<V,K2> grouper)
Returns a new
MapStream where the key component is calculated by
applying the specified grouper function to the values of this stream and
the value is a stream consisting of all the entries that produced the
same key. |
boolean |
isParallel()
Returns whether this stream, if a terminal operation were to be executed,
would execute in parallel.
|
Iterator<Map.Entry<K,V>> |
iterator()
Returns an iterator for the elements of this stream.
|
Stream<K> |
keys()
Returns a stream of only the keys in this
MapStream . |
MapStream<K,V> |
limit(long maxSize)
Returns a stream consisting of the elements of this stream, truncated
to be no longer than
maxSize in length. |
<R> Stream<R> |
map(BiFunction<? super K,? super V,? extends R> mapper)
Returns a stream consisting of the results of applying the given
function to the elements of this stream.
|
<R> Stream<R> |
map(Function<? super Map.Entry<K,V>,? extends R> mapper)
Returns a stream consisting of the results of applying the given
function to the elements of this stream.
|
<R> MapStream<R,V> |
mapKey(BiFunction<? super K,? super V,? extends R> mapper)
Returns a stream consisting of the results of applying the given
function to the key-component of this stream.
|
<R> MapStream<R,V> |
mapKey(Function<? super K,? extends R> mapper)
Returns a stream consisting of the results of applying the given
function to the key-component of this stream.
|
DoubleStream |
mapToDouble(ToDoubleBiFunction<? super K,? super V> mapper)
Returns a
DoubleStream consisting of the results of applying the
given function to the elements of this stream. |
DoubleStream |
mapToDouble(ToDoubleFunction<? super Map.Entry<K,V>> mapper)
Returns a
DoubleStream consisting of the results of applying the
given function to the elements of this stream. |
IntStream |
mapToInt(ToIntBiFunction<? super K,? super V> mapper)
Returns an
IntStream consisting of the results of applying the
given function to the elements of this stream. |
IntStream |
mapToInt(ToIntFunction<? super Map.Entry<K,V>> mapper)
Returns an
IntStream consisting of the results of applying the
given function to the elements of this stream. |
LongStream |
mapToLong(ToLongBiFunction<? super K,? super V> mapper)
Returns a
LongStream consisting of the results of applying the
given function to the elements of this stream. |
LongStream |
mapToLong(ToLongFunction<? super Map.Entry<K,V>> mapper)
Returns a
LongStream consisting of the results of applying the
given function to the elements of this stream. |
<R> MapStream<K,R> |
mapValue(BiFunction<? super K,? super V,? extends R> mapper)
Returns a stream consisting of the results of applying the given
function to the value-component of this stream.
|
<R> MapStream<K,R> |
mapValue(Function<? super V,? extends R> mapper)
Returns a stream consisting of the results of applying the given
function to the value-component of this stream.
|
Optional<Map.Entry<K,V>> |
max(Comparator<? super Map.Entry<K,V>> comparator)
Returns the maximum element of this stream according to the provided
Comparator . |
Optional<Map.Entry<K,V>> |
maxByKey(Comparator<K> comparator)
Returns the maximum element of this stream according to the provided
Comparator applied to the key-components of this stream. |
Optional<Map.Entry<K,V>> |
maxByValue(Comparator<V> comparator)
Returns the maximum element of this stream according to the provided
Comparator applied to the value-components of this stream. |
Optional<Map.Entry<K,V>> |
min(Comparator<? super Map.Entry<K,V>> comparator)
Returns the minimum element of this stream according to the provided
Comparator . |
Optional<Map.Entry<K,V>> |
minByKey(Comparator<K> comparator)
Returns the minimum element of this stream according to the provided
Comparator applied to the key-components of this stream. |
Optional<Map.Entry<K,V>> |
minByValue(Comparator<V> comparator)
Returns the minimum element of this stream according to the provided
Comparator applied to the value-components of this stream. |
boolean |
noneMatch(BiPredicate<? super K,? super V> predicate)
Returns whether no key-value pairs of this stream match the provided
predicate.
|
boolean |
noneMatch(Predicate<? super Map.Entry<K,V>> predicate)
Returns whether no elements of this stream match the provided predicate.
|
static <K,V> MapStream<K,V> |
of(Map.Entry<K,V>... entries)
Constructs a new
MapStream from an array of entries. |
static <K,V> MapStream<K,V> |
of(Map.Entry<K,V> entry)
Constructs a new
MapStream from a single entry. |
static <K,V> MapStream<K,V> |
of(Map<K,V> map)
Constructs a new
MapStream from a map of key-value pairs. |
static <K,V> MapStream<K,V> |
of(Stream<Map.Entry<K,V>> stream)
Constructs a new
MapStream by wrapping a key-value pair stream. |
MapStream<K,V> |
onClose(Runnable closeHandler)
Returns an equivalent stream with an additional close handler.
|
MapStream<K,V> |
parallel()
Returns an equivalent stream that is parallel.
|
MapStream<K,V> |
peek(BiConsumer<? super K,? super V> action)
Returns a stream consisting of the key-value pairs of this stream,
additionally performing the provided action on each element as elements
are consumed from the resulting stream.
|
MapStream<K,V> |
peek(Consumer<? super Map.Entry<K,V>> action)
Returns a stream consisting of the elements of this stream, additionally
performing the provided action on each element as elements are consumed
from the resulting stream.
|
Optional<Map.Entry<K,V>> |
reduce(BinaryOperator<Map.Entry<K,V>> accumulator)
Performs a reduction on the elements of this stream, using an associative
accumulation function, and returns an
Optional describing the
reduced value, if any. |
Map.Entry<K,V> |
reduce(Map.Entry<K,V> identity,
BinaryOperator<Map.Entry<K,V>> accumulator)
Performs a reduction on the elements of this stream, using the provided
identity value and an associative accumulation function, and returns the
reduced value.
|
<U> U |
reduce(U identity,
BiFunction<U,? super Map.Entry<K,V>,U> accumulator,
BinaryOperator<U> combiner)
Performs a reduction on the elements of this stream, using the provided
identity, accumulation and combining functions.
|
MapStream<K,V> |
sequential()
Returns an equivalent stream that is sequential.
|
MapStream<K,V> |
skip(long n)
Returns a stream consisting of the remaining elements of this stream
after discarding the first
n elements of the stream. |
MapStream<K,V> |
sorted()
Returns a stream consisting of the elements of this stream, sorted
according to natural order.
|
MapStream<K,V> |
sorted(Comparator<? super Map.Entry<K,V>> comparator)
Returns a stream consisting of the elements of this stream, sorted
according to the provided
Comparator . |
MapStream<K,V> |
sortedByKey(Comparator<K> comparator)
Returns a stream consisting of the elements of this stream, sorted
according to the provided
Comparator applied to the keys of the
entries. |
MapStream<K,V> |
sortedByValue(Comparator<V> comparator)
Returns a stream consisting of the elements of this stream, sorted
according to the provided
Comparator applied to the values of the
entries. |
Spliterator<Map.Entry<K,V>> |
spliterator()
Returns a spliterator for the elements of this stream.
|
static <T> BinaryOperator<T> |
throwingMerger()
Returns a merge function, suitable for use in
Map.merge() or
toMap() ,
which always throws IllegalStateException . |
Object[] |
toArray()
Returns an array containing the elements of this stream.
|
<A> A[] |
toArray(IntFunction<A[]> generator)
Returns an array containing the elements of this stream, using the
provided
generator function to allocate the returned array, as
well as any additional arrays that might be required for a partitioned
execution or for resizing. |
Map<K,V> |
toConcurrentMap()
Accumulates all key-value pairs of this
MapStream into a
ConcurrentMap . |
Map<K,V> |
toConcurrentMap(BinaryOperator<V> mergeFunction)
Accumulates all key-value pairs of this
MapStream into a
ConcurrentMap . |
ConcurrentNavigableMap<K,V> |
toConcurrentNavigableMap()
Accumulates all key-value pairs of this
MapStream into a
ConcurrentNavigableMap . |
ConcurrentNavigableMap<K,V> |
toConcurrentNavigableMap(BinaryOperator<V> mergeFunction)
Accumulates all key-value pairs of this
MapStream into a
ConcurrentNavigableMap . |
ConcurrentNavigableMap<K,V> |
toConcurrentNavigableMap(Comparator<K> keyComparator,
BinaryOperator<V> mergeFunction)
Accumulates all key-value pairs of this
MapStream into a
ConcurrentNavigableMap , sorting the entries by applying the
specified comparator to the keys of the stream. |
ConcurrentNavigableMap<K,V> |
toConcurrentNavigableMapByKey(Comparator<K> keyComparator)
Accumulates all key-value pairs of this
MapStream into a
ConcurrentNavigableMap , sorting the entries by applying the
specified comparator to the keys of the stream. |
List<Map.Entry<K,V>> |
toList()
Accumulates all entries of this
MapStream into a List . |
Map<K,V> |
toMap()
Accumulates all key-value pairs of this
MapStream into a Map . |
Map<K,V> |
toMap(BinaryOperator<V> mergeFunction)
Accumulates all key-value pairs of this
MapStream into a Map . |
SortedMap<K,V> |
toSortedMap()
Accumulates all key-value pairs of this
MapStream into a
SortedMap . |
SortedMap<K,V> |
toSortedMap(BinaryOperator<V> mergeFunction)
Accumulates all key-value pairs of this
MapStream into a
SortedMap . |
SortedMap<K,V> |
toSortedMap(Comparator<K> keyComparator,
BinaryOperator<V> mergeFunction)
Accumulates all key-value pairs of this
MapStream into a
SortedMap , sorting the entries by applying the specified
comparator to the keys of the stream. |
SortedMap<K,V> |
toSortedMapByKey(Comparator<K> keyComparator)
Accumulates all key-value pairs of this
MapStream into a
SortedMap , sorting the entries by applying the specified
comparator to the keys of the stream. |
MapStream<K,V> |
unordered()
Returns an equivalent stream that is
unordered.
|
Stream<V> |
values()
Returns a stream of only the values in this
MapStream . |
public static <K,V> MapStream<K,V> of(Map.Entry<K,V> entry)
MapStream
from a single entry.@SafeVarargs public static <K,V> MapStream<K,V> of(Map.Entry<K,V>... entries)
MapStream
from an array of entries.public static <K,V> MapStream<K,V> of(Map<K,V> map)
MapStream
from a map of key-value pairs. The map
will not be affected.
This is equivalent to writing:
MapStream.of(map.entrySet().stream());
public static <K,V> MapStream<K,V> of(Stream<Map.Entry<K,V>> stream)
MapStream
by wrapping a key-value pair stream.
The provided stream will be wrapped in this stream. After this method is
called, no more changes should be done to the stream!public static <K,V> MapStream<K,V> fromKeys(Stream<K> keys, Function<K,V> valueFromKey)
MapStream
by wrapping a key-stream and
calculating the corresponding values using the supplied function.K
- key typeV
- value typekeys
- stream of keys to wrapvalueFromKey
- method for calculating values from keysMapStream
public static <K,V> MapStream<K,V> fromValues(Stream<V> values, Function<V,K> keyFromValue)
MapStream
by wrapping a value-stream and
calculating the corresponding keys using the supplied function.K
- key typeV
- value typevalues
- stream of values to wrapkeyFromValue
- method for calculating keys from valuesMapStream
public static <E,K,V> MapStream<K,V> fromStream(Stream<E> stream, Function<E,K> keyMapper, Function<E,V> valueMapper)
MapStream
by wrapping a stream of objects and
calculating both keys and values using the supplied functions.E
- original stream typeK
- key typeV
- value typestream
- original streamkeyMapper
- method for calculating keysvalueMapper
- method for calculating valuesMapStream
public static <K,V> MapStream<K,V> empty()
MapStream
.public static <K,V> MapStream<V,K> flip(MapStream<K,V> original)
K
- original key typeV
- original value typeoriginal
- original MapStreampublic MapStream<K,V> filter(Predicate<? super Map.Entry<K,V>> predicate)
This is an intermediate operation.
public MapStream<K,V> filter(BiPredicate<? super K,? super V> predicate)
This is an intermediate operation.
predicate
- a non-interfering, stateless predicate to apply to each
key-value pair to determine if it should be includedpublic MapStream<K,V> filterKey(Predicate<? super K> predicate)
This is an intermediate operation.
predicate
- a non-interfering, stateless predicate to apply to each
key to determine if the entry should be includedpublic MapStream<K,V> filterValue(Predicate<? super V> predicate)
This is an intermediate operation.
predicate
- a non-interfering, stateless predicate to apply to each
value to determine if the entry should be includedpublic <R> Stream<R> map(Function<? super Map.Entry<K,V>,? extends R> mapper)
This is an intermediate operation.
public <R> Stream<R> map(BiFunction<? super K,? super V,? extends R> mapper)
This is an intermediate operation.
R
- the element type of the new streammapper
- a non-interfering, stateless function to apply to each
key-value pairpublic <R> MapStream<R,V> mapKey(BiFunction<? super K,? super V,? extends R> mapper)
This is an intermediate operation.
R
- the element type of the new streammapper
- a non-interfering, stateless function to apply to each
key in the streampublic <R> MapStream<R,V> mapKey(Function<? super K,? extends R> mapper)
This is an intermediate operation.
R
- the element type of the new streammapper
- a non-interfering, stateless function to apply to each
key in the streampublic <R> MapStream<K,R> mapValue(BiFunction<? super K,? super V,? extends R> mapper)
This is an intermediate operation.
R
- the element type of the new streammapper
- a non-interfering, stateless function to apply to each
value in the streampublic <R> MapStream<K,R> mapValue(Function<? super V,? extends R> mapper)
This is an intermediate operation.
R
- the element type of the new streammapper
- a non-interfering, stateless function to apply to each
value in the streampublic IntStream mapToInt(ToIntFunction<? super Map.Entry<K,V>> mapper)
IntStream
consisting of the results of applying the
given function to the elements of this stream.
This is an intermediate operation.
public IntStream mapToInt(ToIntBiFunction<? super K,? super V> mapper)
IntStream
consisting of the results of applying the
given function to the elements of this stream.
This is an intermediate operation.
mapper
- a non-interfering, stateless function to apply to each
elementpublic LongStream mapToLong(ToLongFunction<? super Map.Entry<K,V>> mapper)
LongStream
consisting of the results of applying the
given function to the elements of this stream.
This is an intermediate operation.
public LongStream mapToLong(ToLongBiFunction<? super K,? super V> mapper)
LongStream
consisting of the results of applying the
given function to the elements of this stream.
This is an intermediate operation.
mapper
- a non-interfering, stateless function to apply to each
elementpublic DoubleStream mapToDouble(ToDoubleFunction<? super Map.Entry<K,V>> mapper)
DoubleStream
consisting of the results of applying the
given function to the elements of this stream.
This is an intermediate operation.
mapToDouble
in interface Stream<Map.Entry<K,V>>
mapper
- a non-interfering, stateless function to apply to each
elementpublic DoubleStream mapToDouble(ToDoubleBiFunction<? super K,? super V> mapper)
DoubleStream
consisting of the results of applying the
given function to the elements of this stream.
This is an intermediate operation.
mapper
- a non-interfering, stateless function to apply to each
elementpublic <R> Stream<R> flatMap(Function<? super Map.Entry<K,V>,? extends Stream<? extends R>> mapper)
closed
after its contents
have been placed into this stream. (If a mapped stream is null
an empty stream is used, instead.)
This is an intermediate operation.
The flatMap()
operation has the effect of applying a one-to-many
transformation to the elements of the stream, and then flattening the
resulting elements into a new stream.
Examples.
If orders
is a stream of purchase orders, and each purchase
order contains a collection of line items, then the following produces a
stream containing all the line items in all the orders:
orders.flatMap(order -> order.getLineItems().stream())...
If path
is the path to a file, then the following produces a
stream of the words
contained in that file:
Stream<String> lines = Files.lines(path, StandardCharsets.UTF_8);
Stream<String> words = lines.flatMap(line -> Stream.of(line.split(" +")));
The mapper
function passed to flatMap
splits a line,
using a simple regular expression, into an array of words, and then
creates a stream of words from that array.public <R> Stream<R> flatMap(BiFunction<? super K,? super V,? extends Stream<? extends R>> mapper)
closed
after its contents
have been placed into this stream. (If a mapped stream is null
an empty stream is used, instead.)
This is an intermediate operation.
The flatMap()
operation has the effect of applying a one-to-many
transformation to the elements of the stream, and then flattening the
resulting elements into a new stream.
Examples.
If orders
is a stream of purchase orders, and each purchase
order contains a collection of line items, then the following produces a
stream containing all the line items in all the orders:
orders.flatMap(order -> order.getLineItems().stream())...
If path
is the path to a file, then the following produces a
stream of the words
contained in that file:
Stream<String> lines = Files.lines(path, StandardCharsets.UTF_8);
Stream<String> words = lines.flatMap(line -> Stream.of(line.split(" +")));
The mapper
function passed to flatMap
splits a line,
using a simple regular expression, into an array of words, and then
creates a stream of words from that array.R
- the element type of the new streammapper
- a non-interfering, stateless function to apply to each
element which produces a stream of new elementspublic <R> MapStream<R,V> flatMapKey(BiFunction<? super K,? super V,? extends Stream<? extends R>> mapper)
closed
after its contents have been placed into this stream. (If a mapped stream
is null
an empty stream is used, instead.)
This is an intermediate operation.
The flatMap()
operation has the effect of applying a one-to-many
transformation to the elements of the stream, and then flattening the
resulting elements into a new stream.
R
- the element type of the new streammapper
- a non-interfering, stateless function to apply to each
key-value pair which produces a stream of new keyspublic <R> MapStream<R,V> flatMapKey(Function<? super K,? extends Stream<? extends R>> mapper)
closed
after its contents have been placed into this stream. (If a mapped stream
is null
an empty stream is used, instead.)
This is an intermediate operation.
The flatMap()
operation has the effect of applying a one-to-many
transformation to the elements of the stream, and then flattening the
resulting elements into a new stream.
R
- the element type of the new streammapper
- a non-interfering, stateless function to apply to each
key-value pair which produces a stream of new keyspublic <R> MapStream<K,R> flatMapValue(BiFunction<? super K,? super V,? extends Stream<? extends R>> mapper)
closed
after its contents
have been placed into this stream. (If a mapped stream is null
an
empty stream is used, instead.)
This is an intermediate operation.
The flatMap()
operation has the effect of applying a one-to-many
transformation to the elements of the stream, and then flattening the
resulting elements into a new stream.
R
- the element type of the new streammapper
- a non-interfering, stateless function to apply to each
key-value pair which produces a stream of new valuespublic <R> MapStream<K,R> flatMapValue(Function<? super V,? extends Stream<? extends R>> mapper)
closed
after its contents
have been placed into this stream. (If a mapped stream is null
an
empty stream is used, instead.)
This is an intermediate operation.
The flatMap()
operation has the effect of applying a one-to-many
transformation to the elements of the stream, and then flattening the
resulting elements into a new stream.
R
- the element type of the new streammapper
- a non-interfering, stateless function to apply to each
key-value pair which produces a stream of new valuespublic IntStream flatMapToInt(Function<? super Map.Entry<K,V>,? extends IntStream> mapper)
IntStream
consisting of the results of replacing each
element of this stream with the contents of a mapped stream produced by
applying the provided mapping function to each element. Each mapped
stream is closed
after its
contents have been placed into this stream. (If a mapped stream is
null
an empty stream is used, instead.)
This is an intermediate operation.
flatMapToInt
in interface Stream<Map.Entry<K,V>>
mapper
- a non-interfering, stateless function to apply to each
element which produces a stream of new elementsflatMap(Function)
public IntStream flatMapToInt(BiFunction<? super K,? super V,? extends IntStream> mapper)
IntStream
consisting of the results of replacing each
key-value pair of this stream with the contents of a mapped stream
produced by applying the provided mapping function to each element. Each
mapped stream is closed
after
its contents have been placed into this stream. (If a mapped stream is
null
an empty stream is used, instead.)
This is an intermediate operation.
mapper
- a non-interfering, stateless function to apply to each
key-value pair which produces a stream of new elementsflatMap(Function)
public LongStream flatMapToLong(Function<? super Map.Entry<K,V>,? extends LongStream> mapper)
LongStream
consisting of the results of replacing each
element of this stream with the contents of a mapped stream produced by
applying the provided mapping function to each element. Each mapped
stream is closed
after its
contents have been placed into this stream. (If a mapped stream is
null
an empty stream is used, instead.)
This is an intermediate operation.
flatMapToLong
in interface Stream<Map.Entry<K,V>>
mapper
- a non-interfering, stateless function to apply to each
element which produces a stream of new elementsflatMap(Function)
public LongStream flatMapToLong(BiFunction<? super K,? super V,? extends LongStream> mapper)
LongStream
consisting of the results of replacing each
key-value pair of this stream with the contents of a mapped stream
produced by applying the provided mapping function to each element. Each
mapped stream is closed
after
its contents have been placed into this stream. (If a mapped stream is
null
an empty stream is used, instead.)
This is an intermediate operation.
mapper
- a non-interfering, stateless function to apply to each
key-value pair which produces a stream of new elementsflatMap(Function)
public DoubleStream flatMapToDouble(Function<? super Map.Entry<K,V>,? extends DoubleStream> mapper)
DoubleStream
consisting of the results of replacing
each element of this stream with the contents of a mapped stream produced
by applying the provided mapping function to each element. Each mapped
stream is closed
after its
contents have placed been into this stream. (If a mapped stream is
null
an empty stream is used, instead.)
This is an intermediate operation.
flatMapToDouble
in interface Stream<Map.Entry<K,V>>
mapper
- a non-interfering, stateless function to apply to each
element which produces a stream of new elementsflatMap(Function)
public DoubleStream flatMapToDouble(BiFunction<? super K,? super V,? extends DoubleStream> mapper)
DoubleStream
consisting of the results of replacing
each key-value pair of this stream with the contents of a mapped stream
produced by applying the provided mapping function to each element. Each
mapped stream is closed
after
its contents have placed been into this stream. (If a mapped stream is
null
an empty stream is used, instead.)
This is an intermediate operation.
mapper
- a non-interfering, stateless function to apply to each
key-value pair which produces a stream of new elementsflatMap(Function)
public Stream<K> keys()
MapStream
.
This is an intermediate operation.
public Stream<V> values()
MapStream
.
This is an intermediate operation.
public MapStream<K,V> distinct()
Object.equals(Object)
) of this stream.
For ordered streams, the selection of distinct elements is stable (for duplicated elements, the element appearing first in the encounter order is preserved.) For unordered streams, no stability guarantees are made.
This is a stateful intermediate operation.
Preserving stability for distinct()
in parallel pipelines is
relatively expensive (requires that the operation act as a full barrier,
with substantial buffering overhead), and stability is often not needed.
Using an unordered stream source (such as Stream.generate(Supplier)
)
or removing the ordering constraint with unordered()
may result
in significantly more efficient execution for distinct()
in parallel
pipelines, if the semantics of your situation permit. If consistency
with encounter order is required, and you are experiencing poor performance
or memory utilization with distinct()
in parallel pipelines,
switching to sequential execution with sequential()
may improve
performance.
public MapStream<K,V> distinctKeys()
Object.equals(Object)
) of this stream based on the key only. If
the same key is encountered multiple times, only the first occurence will
be allowed to pass.
This is a stateful intermediate operation.
public MapStream<K,V> distinctValues()
Object.equals(Object)
) of this stream based on the value only. If
the same value is encountered multiple times, only the first occurence
will be allowed to pass.
This is a stateful intermediate operation.
public MapStream<K,V> distinctKeys(BinaryOperator<V> merger)
Object.equals(Object)
) of this stream based on the key only. If
the same key is encountered multiple times, the specified value merger
will be used to determine which value can pass.
This operation will consume the wrapped stream and produce a new one. The complexity of this operation is therefore O(n).
This is a stateful intermediate operation.
merger
- the merging operation to usepublic MapStream<K,V> distinctValues(BinaryOperator<K> merger)
Object.equals(Object)
) of this stream based on the value only. If
the same value is encountered multiple times, the specified key merger
will be used to determine which value can pass.
This operation will consume the wrapped stream and produce a new one. The complexity of this operation is therefore O(n).
This is a stateful intermediate operation.
merger
- the merging operation to usepublic MapStream<K,V> sorted()
Comparable
, a java.lang.ClassCastException
may be thrown
when the terminal operation is executed.
For ordered streams, the sort is stable. For unordered streams, no stability guarantees are made.
This is a stateful intermediate operation.
public MapStream<K,V> sorted(Comparator<? super Map.Entry<K,V>> comparator)
Comparator
.
For ordered streams, the sort is stable. For unordered streams, no stability guarantees are made.
This is a stateful intermediate operation.
public MapStream<K,V> sortedByKey(Comparator<K> comparator)
Comparator
applied to the keys of the
entries.
For ordered streams, the sort is stable. For unordered streams, no stability guarantees are made.
This is a stateful intermediate operation.
comparator
- a non-interfering, stateless Comparator
to be
used to compare entity keyspublic MapStream<K,V> sortedByValue(Comparator<V> comparator)
Comparator
applied to the values of the
entries.
For ordered streams, the sort is stable. For unordered streams, no stability guarantees are made.
This is a stateful intermediate operation.
comparator
- a non-interfering, stateless Comparator
to be
used to compare entity valuespublic MapStream<K,V> peek(Consumer<? super Map.Entry<K,V>> action)
This is an intermediate operation.
For parallel stream pipelines, the action may be called at whatever time and in whatever thread the element is made available by the upstream operation. If the action modifies shared state, it is responsible for providing the required synchronization.
This method exists mainly to support debugging, where you want to see the elements as they flow past a certain point in a pipeline:
Stream.of("one", "two", "three", "four")
.filter(e -> e.length() > 3)
.peek(e -> System.out.println("Filtered value: " + e))
.map(String::toUpperCase)
.peek(e -> System.out.println("Mapped value: " + e))
.collect(Collectors.toList());
public MapStream<K,V> peek(BiConsumer<? super K,? super V> action)
This is an intermediate operation.
For parallel stream pipelines, the action may be called at whatever time and in whatever thread the element is made available by the upstream operation. If the action modifies shared state, it is responsible for providing the required synchronization.
This method exists mainly to support debugging, where you want to see the elements as they flow past a certain point in a pipeline:
Stream.of("one", "two", "three", "four")
.filter(e -> e.length() > 3)
.peek(e -> System.out.println("Filtered value: " + e))
.map(String::toUpperCase)
.peek(e -> System.out.println("Mapped value: " + e))
.collect(Collectors.toList());
action
- a non-interfering action to perform on the key-value pairs
as they are consumed from the streampublic MapStream<K,V> limit(long maxSize)
maxSize
in length.
This is a short-circuiting stateful intermediate operation.
While limit()
is generally a cheap operation on sequential
stream pipelines, it can be quite expensive on ordered parallel pipelines,
especially for large values of maxSize
, since limit(n)
is constrained to return not just any n elements, but the
first n elements in the encounter order. Using an unordered
stream source (such as Stream.generate(Supplier)
) or removing the
ordering constraint with unordered()
may result in significant
speedups of limit()
in parallel pipelines, if the semantics of
your situation permit. If consistency with encounter order is required,
and you are experiencing poor performance or memory utilization with
limit()
in parallel pipelines, switching to sequential execution
with sequential()
may improve performance.
public MapStream<K,V> skip(long n)
n
elements of the stream.
If this stream contains fewer than n
elements then an
empty stream will be returned.
This is a stateful intermediate operation.
While skip()
is generally a cheap operation on sequential
stream pipelines, it can be quite expensive on ordered parallel pipelines,
especially for large values of n
, since skip(n)
is constrained to skip not just any n elements, but the
first n elements in the encounter order. Using an unordered
stream source (such as Stream.generate(Supplier)
) or removing the
ordering constraint with unordered()
may result in significant
speedups of skip()
in parallel pipelines, if the semantics of
your situation permit. If consistency with encounter order is required,
and you are experiencing poor performance or memory utilization with
skip()
in parallel pipelines, switching to sequential execution
with sequential()
may improve performance.
public void forEach(Consumer<? super Map.Entry<K,V>> action)
This is a terminal operation.
The behavior of this operation is explicitly nondeterministic. For parallel stream pipelines, this operation does not guarantee to respect the encounter order of the stream, as doing so would sacrifice the benefit of parallelism. For any given element, the action may be performed at whatever time and in whatever thread the library chooses. If the action accesses shared state, it is responsible for providing the required synchronization.
public void forEach(BiConsumer<? super K,? super V> action)
This is a terminal operation.
The behavior of this operation is explicitly nondeterministic. For parallel stream pipelines, this operation does not guarantee to respect the encounter order of the stream, as doing so would sacrifice the benefit of parallelism. For any given element, the action may be performed at whatever time and in whatever thread the library chooses. If the action accesses shared state, it is responsible for providing the required synchronization.
action
- a non-interfering action to perform on the key-value pairspublic void forEachOrdered(Consumer<? super Map.Entry<K,V>> action)
This is a terminal operation.
This operation processes the elements one at a time, in encounter order if one exists. Performing the action for one element happens-before performing the action for subsequent elements, but for any given element, the action may be performed in whatever thread the library chooses.
forEachOrdered
in interface Stream<Map.Entry<K,V>>
action
- a non-interfering action to perform on the elementsforEach(Consumer)
public void forEachOrdered(BiConsumer<? super K,? super V> action)
This is a terminal operation.
This operation processes the key-value pairs one at a time, in encounter order if one exists. Performing the action for one element happens-before performing the action for subsequent key-value pairs, but for any given element, the action may be performed in whatever thread the library chooses.
action
- a non-interfering action to perform on the key-value pairsforEach(Consumer)
public Object[] toArray()
This is a terminal operation.
public <A> A[] toArray(IntFunction<A[]> generator)
generator
function to allocate the returned array, as
well as any additional arrays that might be required for a partitioned
execution or for resizing.
This is a terminal operation.
The generator function takes an integer, which is the size of the desired array, and produces an array of the desired size. This can be concisely expressed with an array constructor reference:
Person[] men = people.stream()
.filter(p -> p.getGender() == MALE)
.toArray(Person[]::new);
toArray
in interface Stream<Map.Entry<K,V>>
A
- the element type of the resulting arraygenerator
- a function which produces a new array of the desired
type and the provided lengthArrayStoreException
- if the runtime type of the array
returned from the array generator is not a supertype of
the runtime type of every element in this streampublic Map.Entry<K,V> reduce(Map.Entry<K,V> identity, BinaryOperator<Map.Entry<K,V>> accumulator)
T result = identity;
for (T element : this stream)
result = accumulator.apply(result, element)
return result;
but is not constrained to execute sequentially.
The identity
value must be an identity for the accumulator
function. This means that for all t
,
accumulator.apply(identity, t)
is equal to t
.
The accumulator
function must be an associative function.
This is a terminal operation.
Sum, min, max, average, and string concatenation are all special cases of reduction. Summing a stream of numbers can be expressed as:
Integer sum = integers.reduce(0, (a, b) -> a+b);
or:
Integer sum = integers.reduce(0, Integer::sum);
While this may seem a more roundabout way to perform an aggregation compared to simply mutating a running total in a loop, reduction operations parallelize more gracefully, without needing additional synchronization and with greatly reduced risk of data races.
public Optional<Map.Entry<K,V>> reduce(BinaryOperator<Map.Entry<K,V>> accumulator)
Optional
describing the
reduced value, if any. This is equivalent to:
boolean foundAny = false;
T result = null;
for (T element : this stream) {
if (!foundAny) {
foundAny = true;
result = element;
}
else
result = accumulator.apply(result, element);
}
return foundAny ? Optional.of(result) : Optional.empty();
but is not constrained to execute sequentially.
The accumulator
function must be an associative function.
This is a terminal operation.
reduce
in interface Stream<Map.Entry<K,V>>
accumulator
- an associative, non-interfering, stateless function
for combining two valuesOptional
describing the result of the
reductionNullPointerException
- if the result of the reduction is nullStream.reduce(Object, BinaryOperator)
,
min(Comparator)
,
max(Comparator)
public <U> U reduce(U identity, BiFunction<U,? super Map.Entry<K,V>,U> accumulator, BinaryOperator<U> combiner)
U result = identity;
for (T element : this stream)
result = accumulator.apply(result, element)
return result;
but is not constrained to execute sequentially.
The identity
value must be an identity for the combiner
function. This means that for all u
, combiner(identity, u)
is equal to u
. Additionally, the combiner
function
must be compatible with the accumulator
function; for all
u
and t
, the following must hold:
combiner.apply(u, accumulator.apply(identity, t)) == accumulator.apply(u, t)
This is a terminal operation.
Many reductions using this form can be represented more simply
by an explicit combination of map
and reduce
operations.
The accumulator
function acts as a fused mapper and accumulator,
which can sometimes be more efficient than separate mapping and reduction,
such as when knowing the previously reduced value allows you to avoid
some computation.
reduce
in interface Stream<Map.Entry<K,V>>
U
- the type of the resultidentity
- the identity value for the combiner functionaccumulator
- an associative, non-interfering, stateless function
for incorporating an additional element into a resultcombiner
- an associative, non-interfering, stateless
function for combining two values, which must be
compatible with the accumulator functionreduce(BinaryOperator)
,
Stream.reduce(Object, BinaryOperator)
public <R> R collect(Supplier<R> supplier, BiConsumer<R,? super Map.Entry<K,V>> accumulator, BiConsumer<R,R> combiner)
ArrayList
, and elements are incorporated by
updating the state of the result rather than by replacing the result.
This produces a result equivalent to:
R result = supplier.get();
for (T element : this stream)
accumulator.accept(result, element);
return result;
Like Stream.reduce(Object, BinaryOperator)
, collect
operations
can be parallelized without requiring additional synchronization.
This is a terminal operation.
There are many existing classes in the JDK whose signatures are
well-suited for use with method references as arguments to collect()
.
For example, the following will accumulate strings into an ArrayList
:
List<String> asList = stringStream.collect(ArrayList::new, ArrayList::add,
ArrayList::addAll);
The following will take a stream of strings and concatenates them into a single string:
String concat = stringStream.collect(StringBuilder::new, StringBuilder::append,
StringBuilder::append)
.toString();
collect
in interface Stream<Map.Entry<K,V>>
R
- type of the resultsupplier
- a function that creates a new result container. For a
parallel execution, this function may be called
multiple times and must return a fresh value each
time.accumulator
- an associative, non-interfering, stateless function
for incorporating an additional element into a resultcombiner
- an associative, non-interfering, stateless function
for combining two values, which must be compatible
with the accumulator functionpublic <R,A> R collect(Collector<? super Map.Entry<K,V>,A,R> collector)
Collector
. A Collector
encapsulates the functions
used as arguments to collect(Supplier, BiConsumer, BiConsumer)
,
allowing for reuse of collection strategies and composition of collect
operations such as multiple-level grouping or partitioning.
If the stream is parallel, and the Collector
is
concurrent
, and either the
stream is unordered or the collector is
unordered
,
then a concurrent reduction will be performed (see Collector
for
details on concurrent reduction.)
This is a terminal operation.
When executed in parallel, multiple intermediate results may be
instantiated, populated, and merged so as to maintain isolation of
mutable data structures. Therefore, even when executed in parallel
with non-thread-safe data structures (such as ArrayList
), no
additional synchronization is needed for a parallel reduction.
The following will accumulate strings into an ArrayList:
List<String> asList = stringStream.collect(Collectors.toList());
The following will classify Person
objects by city:
Map<String, List<Person>> peopleByCity
= personStream.collect(Collectors.groupingBy(Person::getCity));
The following will classify Person
objects by state and city,
cascading two Collector
s together:
Map<String, Map<String, List<Person>>> peopleByStateAndCity
= personStream.collect(Collectors.groupingBy(Person::getState,
Collectors.groupingBy(Person::getCity)));
collect
in interface Stream<Map.Entry<K,V>>
R
- the type of the resultA
- the intermediate accumulation type of the
Collector
collector
- the Collector
describing the reductioncollect(Supplier, BiConsumer, BiConsumer)
,
Collectors
public <K2> MapStream<K2,List<V>> groupingBy(Function<V,K2> grouper)
MapStream
where the key component is calculated by
applying the specified grouper function to the values of this stream and
the value is a stream consisting of all the entries that produced the
same key.
This is a stateful intermediate operation.
K2
- the type of the new keygrouper
- the function to use for grouping entriespublic Optional<Map.Entry<K,V>> min(Comparator<? super Map.Entry<K,V>> comparator)
Comparator
. This is a special case of a reduction.
This is a terminal operation.
min
in interface Stream<Map.Entry<K,V>>
comparator
- a non-interfering, stateless Comparator
to
compare elements of this streamOptional
describing the minimum element of
this stream, or an empty Optional
if the
stream is emptyNullPointerException
- if the minimum element is nullpublic Optional<Map.Entry<K,V>> minByKey(Comparator<K> comparator)
Comparator
applied to the key-components of this stream. This is
a special case of a reduction.
This is a terminal operation.
comparator
- a non-interfering, stateless Comparator
to
compare keys of this streamOptional
describing the minimum element of
this stream, or an empty Optional
if the
stream is emptyNullPointerException
- if the minimum element is nullpublic Optional<Map.Entry<K,V>> minByValue(Comparator<V> comparator)
Comparator
applied to the value-components of this stream. This
is a special case of a reduction.
This is a terminal operation.
comparator
- a non-interfering, stateless Comparator
to
compare values of this streamOptional
describing the minimum element of
this stream, or an empty Optional
if the
stream is emptyNullPointerException
- if the minimum element is nullpublic Optional<Map.Entry<K,V>> max(Comparator<? super Map.Entry<K,V>> comparator)
Comparator
. This is a special case of a reduction.
This is a terminal operation.
max
in interface Stream<Map.Entry<K,V>>
comparator
- a non-interfering, stateless Comparator
to
compare elements of this streamOptional
describing the maximum element of
this stream, or an empty Optional
if the
stream is emptyNullPointerException
- if the maximum element is nullpublic Optional<Map.Entry<K,V>> maxByKey(Comparator<K> comparator)
Comparator
applied to the key-components of this stream. This
is a special case of a reduction.
This is a terminal operation.
comparator
- a non-interfering, stateless Comparator
to
compare keys of this streamOptional
describing the maximum element of
this stream, or an empty Optional
if the
stream is emptyNullPointerException
- if the maximum element is nullpublic Optional<Map.Entry<K,V>> maxByValue(Comparator<V> comparator)
Comparator
applied to the value-components of this stream. This
is a special case of a reduction.
This is a terminal operation.
comparator
- a non-interfering, stateless Comparator
to
compare values of this streamOptional
describing the maximum element of
this stream, or an empty Optional
if the
stream is emptyNullPointerException
- if the maximum element is nullpublic long count()
return mapToLong(e -> 1L).sum();
This is a terminal operation.
public boolean anyMatch(Predicate<? super Map.Entry<K,V>> predicate)
false
is returned and the predicate is not evaluated.
This is a short-circuiting terminal operation.
This method evaluates the existential quantification of the predicate over the elements of the stream (for some x P(x)).
public boolean anyMatch(BiPredicate<? super K,? super V> predicate)
false
is returned and the predicate is not evaluated.
This is a short-circuiting terminal operation.
This method evaluates the existential quantification of the predicate over the elements of the stream (for some x P(x)).
predicate
- a non-interfering, stateless predicate to apply to
key-value pairs of this streamtrue
if any key-value pairs of the stream match
the provided predicate, otherwise false
public boolean allMatch(Predicate<? super Map.Entry<K,V>> predicate)
true
is
returned and the predicate is not evaluated.
This is a short-circuiting terminal operation.
This method evaluates the universal quantification of the
predicate over the elements of the stream (for all x P(x)). If the
stream is empty, the quantification is said to be vacuously
satisfied and is always true
(regardless of P(x)).
public boolean allMatch(BiPredicate<? super K,? super V> predicate)
true
is returned and the predicate is not evaluated.
This is a short-circuiting terminal operation.
This method evaluates the universal quantification of the
predicate over the elements of the stream (for all x P(x)). If the
stream is empty, the quantification is said to be vacuously
satisfied and is always true
(regardless of P(x)).
predicate
- a non-interfering, stateless predicate to apply to
key-value pairs of this streamtrue
if either all key-value pairs of the
stream match the provided predicate or the stream is
empty, otherwise false
public boolean noneMatch(Predicate<? super Map.Entry<K,V>> predicate)
true
is
returned and the predicate is not evaluated.
This is a short-circuiting terminal operation.
This method evaluates the universal quantification of the
negated predicate over the elements of the stream (for all x ~P(x)). If
the stream is empty, the quantification is said to be vacuously satisfied
and is always true
, regardless of P(x).
public boolean noneMatch(BiPredicate<? super K,? super V> predicate)
true
is returned and the predicate is not evaluated.
This is a short-circuiting terminal operation.
This method evaluates the universal quantification of the
negated predicate over the elements of the stream (for all x ~P(x)). If
the stream is empty, the quantification is said to be vacuously satisfied
and is always true
, regardless of P(x).
predicate
- a non-interfering, stateless predicate to apply to
key-value pairs of this streamtrue
if either no key-value pairs of the
stream match the provided predicate or the stream is
empty, otherwise false
public Optional<Map.Entry<K,V>> findFirst()
Optional
describing the first element of this stream,
or an empty Optional
if the stream is empty. If the stream has
no encounter order, then any element may be returned.
This is a short-circuiting terminal operation.
public Optional<Map.Entry<K,V>> findAny()
Optional
describing some element of the stream, or an
empty Optional
if the stream is empty.
This is a short-circuiting terminal operation.
The behavior of this operation is explicitly nondeterministic; it is
free to select any element in the stream. This is to allow for maximal
performance in parallel operations; the cost is that multiple invocations
on the same source may not return the same result. (If a stable result
is desired, use findFirst()
instead.)
findAny
in interface Stream<Map.Entry<K,V>>
Optional
describing some element of this stream, or an
empty Optional
if the stream is emptyNullPointerException
- if the element selected is nullfindFirst()
public Iterator<Map.Entry<K,V>> iterator()
This is a terminal operation.
public Spliterator<Map.Entry<K,V>> spliterator()
This is a terminal operation.
spliterator
in interface BaseStream<Map.Entry<K,V>,Stream<Map.Entry<K,V>>>
public boolean isParallel()
isParallel
in interface BaseStream<Map.Entry<K,V>,Stream<Map.Entry<K,V>>>
true
if this stream would execute in parallel if executedpublic MapStream<K,V> sequential()
This is an intermediate operation.
sequential
in interface BaseStream<Map.Entry<K,V>,Stream<Map.Entry<K,V>>>
public MapStream<K,V> parallel()
This is an intermediate operation.
public MapStream<K,V> unordered()
This is an intermediate operation.
public MapStream<K,V> onClose(Runnable closeHandler)
close()
method
is called on the stream, and are executed in the order they were
added. All close handlers are run, even if earlier close handlers throw
exceptions. If any close handler throws an exception, the first
exception thrown will be relayed to the caller of close()
, with
any remaining exceptions added to that exception as suppressed exceptions
(unless one of the remaining exceptions is the same exception as the
first exception, since an exception cannot suppress itself.) May
return itself.
This is an intermediate operation.
public void close()
close
in interface AutoCloseable
close
in interface BaseStream<Map.Entry<K,V>,Stream<Map.Entry<K,V>>>
AutoCloseable.close()
public Map<K,V> toMap()
MapStream
into a Map
.
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(BinaryOperator)
instead.
The returned Map
is not concurrent. For parallel stream
pipelines, the combiner
function operates by merging the keys
from one map into another, which can be an expensive operation. If it is
not required that results are inserted into the Map
in encounter
order, using toConcurrentMap()
may offer better parallel performance.
Map
whose keys and values are identical to the entries
of this streamtoMap(BinaryOperator)
,
toConcurrentMap()
public Map<K,V> toMap(BinaryOperator<V> mergeFunction)
MapStream
into a Map
.
If the mapped keys contains duplicates (according to
Object.equals(Object)
), the values are merged using the provided
merging function.
There are multiple ways to deal with collisions between multiple elements
mapping to the same key. The other forms of toMap
simply use
a merge function that throws unconditionally, but you can easily write
more flexible merge policies.
The returned Map
is not concurrent. For parallel stream
pipelines, the combiner
function operates by merging the keys
from one map into another, which can be an expensive operation. If it is
not required that results are merged into the Map
in encounter
order, using toConcurrentMap(BinaryOperator)
may offer better parallel performance.
mergeFunction
- a merge function, used to resolve collisions
between values associated with the same key, as
supplied to Map.merge(Object, Object, BiFunction)
Map
whose keys and values are identical
to the entries of this stream with any collisions
handledtoMap()
,
toConcurrentMap(BinaryOperator)
public Map<K,V> toConcurrentMap()
MapStream
into a
ConcurrentMap
.
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(BinaryOperator)
instead.
It is common for either the key or the value to be the input elements.
In this case, the utility method
Function.identity()
may be helpful.
This is a concurrent
and
unordered
Collector.
ConcurrentMap
whose keys are taken from this streamtoMap()
,
toConcurrentMap(BinaryOperator)
public Map<K,V> toConcurrentMap(BinaryOperator<V> mergeFunction)
MapStream
into a
ConcurrentMap
.
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.
There are multiple ways to deal with collisions between multiple elements
mapping to the same key. The other forms of toConcurrentMap
simply use
a merge function that throws unconditionally, but you can easily write
more flexible merge policies.
mergeFunction
- a merge function, used to resolve collisions between
values associated with the same key, as supplied
to Map.merge(Object, Object, BiFunction)
ConcurrentMap
whose keys are taken from this streamtoConcurrentMap()
,
toMap(BinaryOperator)
public SortedMap<K,V> toSortedMap()
MapStream
into a
SortedMap
.
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 toSortedMap(BinaryOperator)
instead.
If the keys are not Comparable
, a
java.lang.ClassCastException
may be thrown when the terminal
operation is executed. It is therefore better to use
toSortedMapByKey(Comparator)
since it allows you to specify
a comparator to use when comparing the keys.
The returned Map
is not concurrent. For parallel stream
pipelines, use toConcurrentNavigableMap()
instead.
Map
whose keys and values are identical to the entries
of this streamtoMap(BinaryOperator)
,
toConcurrentMap()
public SortedMap<K,V> toSortedMapByKey(Comparator<K> keyComparator)
MapStream
into a
SortedMap
, sorting the entries by applying the specified
comparator to the keys of the stream.
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 toSortedMap(Comparator, BinaryOperator)
instead.
The returned Map
is not concurrent. For parallel stream
pipelines, use toConcurrentNavigableMap()
instead.
keyComparator
- the comparator to use when sorting the keysSortedMap
whose keys and values are
identical to the entries of this streamtoMap(BinaryOperator)
,
toConcurrentMap()
,
toSortedMap()
public SortedMap<K,V> toSortedMap(BinaryOperator<V> mergeFunction)
MapStream
into a
SortedMap
.
If the mapped keys contains duplicates (according to
Object.equals(Object)
), the values are merged using the provided
merging function.
There are multiple ways to deal with collisions between multiple elements
mapping to the same key. The other forms of toSortedMap
simply
use a merge function that throws unconditionally, but you can easily
write more flexible merge policies.
The returned SortedMap
is not concurrent. For parallel stream
pipelines, the combiner
function operates by merging the keys
from one map into another, which can be an expensive operation. If it is
not required that results are merged into the Map
in encounter
order, using toConcurrentNavigableMap(BinaryOperator)
may offer better parallel performance.
mergeFunction
- a merge function, used to resolve collisions
between values associated with the same key, as
supplied to Map.merge(Object, Object, BiFunction)
SortedMap
whose keys and values are identical
to the entries of this stream with any collisions
handledtoMap()
,
toSortedMap()
,
toConcurrentMap(BinaryOperator)
public SortedMap<K,V> toSortedMap(Comparator<K> keyComparator, BinaryOperator<V> mergeFunction)
MapStream
into a
SortedMap
, sorting the entries by applying the specified
comparator to the keys of the stream.
If the mapped keys contains duplicates (according to
Object.equals(Object)
), the values are merged using the provided
merging function.
There are multiple ways to deal with collisions between multiple elements
mapping to the same key. The other forms of toSortedMap
simply
use a merge function that throws unconditionally, but you can easily
write more flexible merge policies.
The returned SortedMap
is not concurrent. For parallel stream
pipelines, the combiner
function operates by merging the keys
from one map into another, which can be an expensive operation. If it is
not required that results are merged into the Map
in encounter
order, using toConcurrentNavigableMap(Comparator, BinaryOperator)
may offer better parallel performance.
keyComparator
- the comparator to use for determining the order of
the keysmergeFunction
- a merge function, used to resolve collisions
between values associated with the same key, as
supplied to Map.merge(Object, Object, BiFunction)
SortedMap
whose keys and values are identical
to the entries of this stream with any collisions
handledtoMap()
,
toSortedMap()
,
toConcurrentMap(BinaryOperator)
public ConcurrentNavigableMap<K,V> toConcurrentNavigableMap()
MapStream
into a
ConcurrentNavigableMap
.
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 toConcurrentNavigableMap(BinaryOperator)
instead.
If the keys are not Comparable
, a
java.lang.ClassCastException
may be thrown when the terminal
operation is executed. It is therefore better to use
toConcurrentNavigableMapByKey(Comparator)
since it allows you to specify
a comparator to use when comparing the keys.
ConcurrentNavigableMap
whose keys and values are
identical to the entries of this streamtoMap(BinaryOperator)
,
toConcurrentMap()
public ConcurrentNavigableMap<K,V> toConcurrentNavigableMapByKey(Comparator<K> keyComparator)
MapStream
into a
ConcurrentNavigableMap
, sorting the entries by applying the
specified comparator to the keys of the stream.
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 toConcurrentNavigableMap(Comparator, BinaryOperator)
instead.
keyComparator
- the comparator to use when sorting the keysConcurrentNavigableMap
whose keys and
values are identical to the entries of this streamtoMap(BinaryOperator)
,
toConcurrentMap()
,
toSortedMap()
public ConcurrentNavigableMap<K,V> toConcurrentNavigableMap(BinaryOperator<V> mergeFunction)
MapStream
into a
ConcurrentNavigableMap
.
If the mapped keys contains duplicates (according to
Object.equals(Object)
), the values are merged using the provided
merging function.
There are multiple ways to deal with collisions between multiple elements
mapping to the same key. The other forms of toConcurrentNavigableMap
simply
use a merge function that throws unconditionally, but you can easily
write more flexible merge policies.
mergeFunction
- a merge function, used to resolve collisions
between values associated with the same key, as
supplied to Map.merge(Object, Object, BiFunction)
ConcurrentNavigableMap
whose keys and
values are identical to the entries of this stream
with any collisions handledtoMap()
,
toSortedMap()
,
toConcurrentMap(BinaryOperator)
public ConcurrentNavigableMap<K,V> toConcurrentNavigableMap(Comparator<K> keyComparator, BinaryOperator<V> mergeFunction)
MapStream
into a
ConcurrentNavigableMap
, sorting the entries by applying the
specified comparator to the keys of the stream.
If the mapped keys contains duplicates (according to
Object.equals(Object)
), the values are merged using the provided
merging function.
There are multiple ways to deal with collisions between multiple elements
mapping to the same key. The other forms of
toConcurrentNavigableMap
simply use a merge function that throws
unconditionally, but you can easily write more flexible merge policies.
keyComparator
- the comparator to use for determining the order of
the keysmergeFunction
- a merge function, used to resolve collisions
between values associated with the same key, as
supplied to Map.merge(Object, Object, BiFunction)
ConcurrentNavigableMap
whose keys and
values are identical to the entries of this stream
with any collisions handledtoMap()
,
toSortedMap()
,
toConcurrentMap(BinaryOperator)
public List<Map.Entry<K,V>> toList()
MapStream
into a List
.
The returned List
is not concurrent.
List
that contains all the entities of the stream@SafeVarargs public static <K> Comparator<K> comparing(Function<K,? extends Comparable<?>>... methods)
K
- the type of the key to comparemethods
- methods for comparing the key ordered by prioritypublic static <T> BinaryOperator<T> throwingMerger()
Map.merge()
or
toMap()
,
which always throws IllegalStateException
. This can be used to
enforce the assumption that the elements being collected are distinct.T
- the type of input arguments to the merge functionIllegalStateException
Copyright © 2016 Speedment, Inc.. All rights reserved.