K - the key typeV - the value typepublic final class MapStream<K,V> extends Object implements Stream<Map.Entry<K,V>>
Map.Entry.
This stream wraps a Stream<Map.Entry>, providing convenient methods for
manipulating the keys and values. Unlike a Map, the keys in a MapStream
do not have to be unique, although certain methods will fail if they are not unique.
Stream.Builder<T>| Modifier and Type | Method and Description |
|---|---|
boolean |
allMatch(BiPredicate<? super K,? super V> predicate)
Returns whether all elements of this stream match the provided predicate.
|
boolean |
allMatch(Predicate<? super Map.Entry<K,V>> predicate) |
boolean |
anyMatch(BiPredicate<? super K,? super V> predicate)
Returns whether any elements of this stream match the provided predicate.
|
boolean |
anyMatch(Predicate<? super Map.Entry<K,V>> predicate) |
void |
close() |
<R,A> R |
collect(Collector<? super Map.Entry<K,V>,A,R> collector) |
<R> R |
collect(Supplier<R> supplier,
BiConsumer<R,? super Map.Entry<K,V>> accumulator,
BiConsumer<R,R> combiner) |
static <K,V> MapStream<K,V> |
concat(MapStream<? extends K,? extends V> a,
MapStream<? extends K,? extends V> b)
Creates a stream of map entries whose elements are those of the first stream followed by those of the second
stream.
|
long |
count() |
MapStream<K,V> |
distinct() |
static <K,V> MapStream<K,V> |
empty()
Returns an empty map stream.
|
MapStream<K,V> |
filter(BiFunction<? super K,? super V,Boolean> predicate)
Filters the stream by applying the predicate function to each key and value.
|
MapStream<K,V> |
filter(Predicate<? super Map.Entry<K,V>> predicate) |
<R> MapStream<R,V> |
filterKeys(Class<R> castToClass)
Filters the stream checking the type of each key.
|
MapStream<K,V> |
filterKeys(Predicate<? super K> predicate)
Filters the stream by applying the predicate function to each key.
|
<R> MapStream<K,R> |
filterValues(Class<R> castToClass)
Filters the stream checking the type of each value.
|
MapStream<K,V> |
filterValues(Predicate<? super V> predicate)
Filters the stream by applying the predicate function to each value.
|
Optional<Map.Entry<K,V>> |
findAny() |
Optional<Map.Entry<K,V>> |
findFirst() |
<R> Stream<R> |
flatMap(BiFunction<? super K,? super V,Stream<R>> mapper)
Transforms the entries in the stream by applying a mapper function to each key and value to produce a stream of
elements, and then flattening the resulting stream of streams.
|
<R> Stream<R> |
flatMap(Function<? super Map.Entry<K,V>,? extends Stream<? extends R>> mapper) |
<R> MapStream<R,V> |
flatMapKeys(BiFunction<? super K,? super V,Stream<R>> mapper)
Transforms the keys in the stream by applying a mapper function to each key and value.
|
<R> MapStream<R,V> |
flatMapKeys(Function<? super K,Stream<R>> mapper)
Transforms the keys in the stream by applying a mapper function to each key.
|
DoubleStream |
flatMapToDouble(BiFunction<? super K,? super V,? extends DoubleStream> mapper)
Transforms the entries in the stream to doubles by applying a mapper function to each key and value to produce
a stream of doubles, and then flattening the resulting stream of streams.
|
DoubleStream |
flatMapToDouble(Function<? super Map.Entry<K,V>,? extends DoubleStream> mapper) |
IntStream |
flatMapToInt(BiFunction<? super K,? super V,? extends IntStream> mapper)
Transforms the entries in the stream to integers by applying a mapper function to each key and value to produce
a stream of integers, and then flattening the resulting stream of streams.
|
IntStream |
flatMapToInt(Function<? super Map.Entry<K,V>,? extends IntStream> mapper) |
LongStream |
flatMapToLong(Function<? super Map.Entry<K,V>,? extends LongStream> mapper) |
<R> MapStream<K,R> |
flatMapValues(BiFunction<? super K,? super V,Stream<R>> mapper)
Transforms the values in the stream by applying a mapper function to each key and value.
|
<R> MapStream<K,R> |
flatMapValues(Function<? super V,Stream<R>> mapper)
Transforms the values in the stream by applying a mapper function to each value.
|
void |
forEach(BiConsumer<? super K,? super V> action)
Performs an action for each entry in the stream, passing the key and value to the action.
|
void |
forEach(Consumer<? super Map.Entry<K,V>> action) |
void |
forEachOrdered(Consumer<? super Map.Entry<K,V>> action) |
boolean |
isParallel() |
Iterator<Map.Entry<K,V>> |
iterator() |
Stream<K> |
keys()
Returns the keys as a stream, dropping the values.
|
MapStream<K,V> |
limit(long maxSize) |
<R> Stream<R> |
map(BiFunction<? super K,? super V,? extends R> mapper)
Transforms the entries in the stream by applying a mapper function to each key and value.
|
<R> Stream<R> |
map(Function<? super Map.Entry<K,V>,? extends R> mapper) |
<R> MapStream<R,V> |
mapKeys(BiFunction<? super K,? super V,? extends R> mapper)
Transforms the keys in the stream by applying a mapper function to each key and value.
|
<R> MapStream<R,V> |
mapKeys(Function<? super K,? extends R> mapper)
Transforms the keys in the stream by applying a mapper function to each key.
|
DoubleStream |
mapToDouble(ToDoubleBiFunction<? super K,? super V> mapper)
Transforms the entries in the stream to doubles by applying a mapper function to each key and value.
|
DoubleStream |
mapToDouble(ToDoubleFunction<? super Map.Entry<K,V>> mapper) |
IntStream |
mapToInt(ToIntBiFunction<? super K,? super V> mapper)
Transforms the entries in the stream to integers by applying a mapper function to each key and value.
|
IntStream |
mapToInt(ToIntFunction<? super Map.Entry<K,V>> mapper) |
LongStream |
mapToLong(ToLongFunction<? super Map.Entry<K,V>> mapper) |
<R> MapStream<K,R> |
mapValues(BiFunction<? super K,? super V,? extends R> mapper)
Transforms the values in the stream by applying a mapper function to each key and value.
|
<R> MapStream<K,R> |
mapValues(Function<? super V,? extends R> mapper)
Transforms the values in the stream by applying a mapper function to each value.
|
Optional<Map.Entry<K,V>> |
max(Comparator<? super Map.Entry<K,V>> comparator) |
Optional<Map.Entry<K,V>> |
maxKeys(Comparator<? super K> comparator)
Finds the maximum entry in the stream by comparing the keys using the supplied comparator.
|
Optional<Map.Entry<K,V>> |
maxValues(Comparator<? super V> comparator)
Finds the maximum entry in the stream by comparing the values using the supplied comparator.
|
Optional<Map.Entry<K,V>> |
min(Comparator<? super Map.Entry<K,V>> comparator) |
Optional<Map.Entry<K,V>> |
minKeys(Comparator<? super K> comparator)
Finds the minimum entry in the stream by comparing the keys using the supplied comparator.
|
Optional<Map.Entry<K,V>> |
minValues(Comparator<? super V> comparator)
Finds the minimum entry in the stream by comparing the values using the supplied comparator.
|
boolean |
noneMatch(BiPredicate<? super K,? super V> predicate)
Returns whether no elements of this stream match the provided predicate.
|
boolean |
noneMatch(Predicate<? super Map.Entry<K,V>> predicate) |
static <T,K,V> MapStream<K,V> |
of(Collection<T> collection,
Function<? super T,? extends K> keyFunction,
Function<? super T,? extends V> valueFunction)
Returns a stream of map entries where the keys and values are extracted from a
collection by applying a function to each item in the collection.
|
static <V> MapStream<V,V> |
of(Collection<V> collection)
Returns a stream of map entries where the keys and values are taken from a collection.
|
static <K,V> MapStream<K,V> |
of(Collection<V> collection,
Function<? super V,? extends K> keyFunction)
Returns a stream of map entries where the values are taken from a collection
and the keys are created by applying a function to each value.
|
static <K,V> MapStream<K,V> |
of(Map<K,V> map)
Returns a stream over the entries in the map.
|
static <K,V> MapStream<K,V> |
of(Multimap<K,V> multimap)
Returns a stream over all the entries in the multimap.
|
static <T,K,V> MapStream<K,V> |
of(Stream<T> stream,
Function<? super T,? extends K> keyFunction,
Function<? super T,? extends V> valueFunction)
Returns a stream of map entries where the keys and values are extracted from a
stream by applying a function to each item in the stream.
|
static <V> MapStream<V,V> |
of(Stream<V> stream)
Returns a stream of map entries where the keys and values are taken from a stream.
|
static <K,V> MapStream<K,V> |
of(Stream<V> stream,
Function<? super V,? extends K> keyFunction)
Returns a stream of map entries where the values are taken from a stream
and the keys are created by applying a function to each value.
|
MapStream<K,V> |
onClose(Runnable closeHandler) |
MapStream<K,V> |
parallel() |
MapStream<K,V> |
peek(Consumer<? super Map.Entry<K,V>> action) |
Optional<Map.Entry<K,V>> |
reduce(BinaryOperator<Map.Entry<K,V>> accumulator) |
Map.Entry<K,V> |
reduce(Map.Entry<K,V> identity,
BinaryOperator<Map.Entry<K,V>> accumulator) |
<U> U |
reduce(U identity,
BiFunction<U,? super Map.Entry<K,V>,U> accumulator,
BinaryOperator<U> combiner) |
MapStream<K,V> |
sequential() |
MapStream<K,V> |
skip(long n) |
MapStream<K,V> |
sorted() |
MapStream<K,V> |
sorted(Comparator<? super Map.Entry<K,V>> comparator) |
MapStream<K,V> |
sortedKeys()
Sorts the entries in the stream by comparing the keys using their natural ordering.
|
MapStream<K,V> |
sortedKeys(Comparator<? super K> comparator)
Sorts the entries in the stream by comparing the keys using the supplied comparator.
|
MapStream<K,V> |
sortedValues()
Sorts the entries in the stream by comparing the values using their natural ordering.
|
MapStream<K,V> |
sortedValues(Comparator<? super V> comparator)
Sorts the entries in the stream by comparing the values using the supplied comparator.
|
Spliterator<Map.Entry<K,V>> |
spliterator() |
Object[] |
toArray() |
<A> A[] |
toArray(IntFunction<A[]> generator) |
ImmutableListMultimap<K,V> |
toListMultimap()
Returns an immutable list multimap built from the entries in the stream.
|
ImmutableMap<K,V> |
toMap()
Returns an immutable map built from the entries in the stream.
|
ImmutableMap<K,V> |
toMap(BiFunction<? super V,? super V,? extends V> mergeFn)
Returns an immutable map built from the entries in the stream.
|
ImmutableMap<K,List<V>> |
toMapGrouping()
Returns an immutable map built from the entries in the stream, grouping by key.
|
<A,R> ImmutableMap<K,R> |
toMapGrouping(Collector<? super V,A,R> valueCollector)
Returns an immutable map built from the entries in the stream, grouping by key.
|
ImmutableSetMultimap<K,V> |
toSetMultimap()
Returns an immutable set multimap built from the entries in the stream.
|
MapStream<K,V> |
unordered() |
Stream<V> |
values()
Returns the values as a stream, dropping the keys.
|
static <K,V> MapStream<K,V> |
zip(Stream<K> keyStream,
Stream<V> valueStream)
Returns a map stream that combines two other streams, continuing until either stream ends.
|
static <V> MapStream<Integer,V> |
zipWithIndex(Stream<V> stream)
Returns a stream of map entries where each key is the index of the value in the original stream.
|
public static <K,V> MapStream<K,V> of(Map<K,V> map)
public static <K,V> MapStream<K,V> of(Multimap<K,V> multimap)
This will typically create a stream with duplicate keys.
public static <V> MapStream<V,V> of(Collection<V> collection)
public static <V> MapStream<V,V> of(Stream<V> stream)
public static <K,V> MapStream<K,V> of(Collection<V> collection, Function<? super V,? extends K> keyFunction)
K - the key typeV - the value typecollection - the collection of valueskeyFunction - a function which returns the key for a valuepublic static <T,K,V> MapStream<K,V> of(Collection<T> collection, Function<? super T,? extends K> keyFunction, Function<? super T,? extends V> valueFunction)
T - the collection typeK - the key typeV - the value typecollection - the collection of valueskeyFunction - a function which returns the keyvalueFunction - a function which returns the valuepublic static <K,V> MapStream<K,V> of(Stream<V> stream, Function<? super V,? extends K> keyFunction)
K - the key typeV - the value typestream - the stream of valueskeyFunction - a function which returns the key for a valuepublic static <T,K,V> MapStream<K,V> of(Stream<T> stream, Function<? super T,? extends K> keyFunction, Function<? super T,? extends V> valueFunction)
T - the collection typeK - the key typeV - the value typestream - the stream of valueskeyFunction - a function which returns the key for a valuevalueFunction - a function which returns the valuepublic static <K,V> MapStream<K,V> zip(Stream<K> keyStream, Stream<V> valueStream)
Note that this can produce a stream with non-unique keys.
K - the key typeV - the value typekeyStream - the stream of keysvalueStream - the stream of valuespublic static <V> MapStream<Integer,V> zipWithIndex(Stream<V> stream)
V - the value typestream - the stream of valuespublic static <K,V> MapStream<K,V> empty()
public static <K,V> MapStream<K,V> concat(MapStream<? extends K,? extends V> a, MapStream<? extends K,? extends V> b)
K - the key typeV - the value typea - the first stream of entriesb - the second stream of entriespublic Stream<K> keys()
A MapStream may contain the same key more than once, so callers
may need to call Stream.distinct() on the result.
public Stream<V> values()
public MapStream<K,V> filter(BiFunction<? super K,? super V,Boolean> predicate)
Entries are included in the returned stream if the predicate function returns true.
predicate - a predicate function applied to each key and value in the streampublic MapStream<K,V> filterKeys(Predicate<? super K> predicate)
Entries are included in the returned stream if the predicate function returns true.
predicate - a predicate function applied to each key in the streampublic <R> MapStream<R,V> filterKeys(Class<R> castToClass)
Entries are included in the returned stream if the key is an instance of the specified type.
R - the type to filter tocastToClass - the class to filter the keys topublic MapStream<K,V> filterValues(Predicate<? super V> predicate)
Entries are included in the returned stream if the predicate function returns true.
predicate - a predicate function applied to each value in the streampublic <R> MapStream<K,R> filterValues(Class<R> castToClass)
Entries are included in the returned stream if the value is an instance of the specified type.
R - the type to filter tocastToClass - the class to filter the values topublic <R> MapStream<R,V> mapKeys(Function<? super K,? extends R> mapper)
The values are unchanged.
R - the type of the new keysmapper - a mapper function whose return value is used as the new keypublic <R> MapStream<R,V> mapKeys(BiFunction<? super K,? super V,? extends R> mapper)
The values are unchanged.
R - the type of the new keysmapper - a mapper function whose return value is used as the new keypublic <R> MapStream<K,R> mapValues(Function<? super V,? extends R> mapper)
The keys are unchanged.
R - the type of the new valuesmapper - a mapper function whose return value is used as the new valuepublic <R> MapStream<K,R> mapValues(BiFunction<? super K,? super V,? extends R> mapper)
The keys are unchanged.
R - the type of the new valuesmapper - a mapper function whose return value is used as the new valuepublic <R> Stream<R> map(BiFunction<? super K,? super V,? extends R> mapper)
R - the type of elements in the new streammapper - a mapper function whose return values are included in the new streampublic DoubleStream mapToDouble(ToDoubleBiFunction<? super K,? super V> mapper)
mapper - a mapper function whose return values are included in the new streampublic IntStream mapToInt(ToIntBiFunction<? super K,? super V> mapper)
mapper - a mapper function whose return values are included in the new streampublic <R> MapStream<R,V> flatMapKeys(Function<? super K,Stream<R>> mapper)
The new keys produced will be associated with the original value.
R - the type of the new keysmapper - a mapper function whose return values are the keys in the new streampublic <R> MapStream<R,V> flatMapKeys(BiFunction<? super K,? super V,Stream<R>> mapper)
The new keys produced will be associated with the original value.
For example this could turn a MapStream<List<String>, LocalDate> into a
MapStream<String, LocalDate>
R - the type of the new keysmapper - a mapper function whose return values are the keys in the new streampublic <R> MapStream<K,R> flatMapValues(Function<? super V,Stream<R>> mapper)
The new values produced will be associated with the original key.
For example this could turn a MapStream<LocalDate, List<String>> into a
MapStream<LocalDate, String>
R - the type of the new valuesmapper - a mapper function whose return values are the values in the new streampublic <R> MapStream<K,R> flatMapValues(BiFunction<? super K,? super V,Stream<R>> mapper)
The new values produced will be associated with the original key.
For example this could turn a MapStream<LocalDate, List<String>> into a
MapStream<LocalDate, String>
R - the type of the new valuesmapper - a mapper function whose return values are the values in the new streampublic <R> Stream<R> flatMap(BiFunction<? super K,? super V,Stream<R>> mapper)
R - the type of the elements in the new streammapper - a mapper function whose return values are included in the new streampublic DoubleStream flatMapToDouble(BiFunction<? super K,? super V,? extends DoubleStream> mapper)
mapper - a mapper function whose return values are included in the new streampublic IntStream flatMapToInt(BiFunction<? super K,? super V,? extends IntStream> mapper)
mapper - a mapper function whose return values are included in the new streampublic MapStream<K,V> sortedKeys()
If the keys in this map stream are not Comparable a java.lang.ClassCastException may be thrown.
In this case use sortedKeys(Comparator) instead.
public MapStream<K,V> sortedKeys(Comparator<? super K> comparator)
comparator - a comparator of keyspublic MapStream<K,V> sortedValues()
If the values in this map stream are not Comparable a java.lang.ClassCastException may be thrown.
In this case use sortedValues(Comparator) instead.
public MapStream<K,V> sortedValues(Comparator<? super V> comparator)
comparator - a comparator of valuespublic Optional<Map.Entry<K,V>> minKeys(Comparator<? super K> comparator)
This is a terminal operation.
comparator - a comparator of keyspublic Optional<Map.Entry<K,V>> minValues(Comparator<? super V> comparator)
This is a terminal operation.
comparator - a comparator of valuespublic Optional<Map.Entry<K,V>> maxKeys(Comparator<? super K> comparator)
This is a terminal operation.
comparator - a comparator of keyspublic Optional<Map.Entry<K,V>> maxValues(Comparator<? super V> comparator)
This is a terminal operation.
comparator - a comparator of valuespublic boolean anyMatch(BiPredicate<? super K,? super V> predicate)
This is a short-circuiting terminal operation.
predicate - the predicate to apply to the entriespublic boolean allMatch(BiPredicate<? super K,? super V> predicate)
This is a short-circuiting terminal operation.
predicate - the predicate to apply to the entriespublic boolean noneMatch(BiPredicate<? super K,? super V> predicate)
This is a short-circuiting terminal operation.
predicate - the predicate to apply to the entriespublic ImmutableMap<K,V> toMap()
The keys must be unique or an exception will be thrown.
Duplicate keys can be handled using toMap(BiFunction).
This is a terminal operation.
IllegalArgumentException - if the same key occurs more than oncepublic ImmutableMap<K,V> toMap(BiFunction<? super V,? super V,? extends V> mergeFn)
If the same key maps to multiple values the merge function is invoked with both values and the return value is used in the map.
Can be used with concat(MapStream, MapStream) to merge immutable
maps with duplicate keys.
For example, to merge immutable maps with duplicate keys preferring values in the first map:
MapStream.concat(mapStreamA, mapStreamB).toMap((a,b) -> a);
This is a terminal operation.
mergeFn - function used to merge values when the same key appears multiple times in the streampublic ImmutableMap<K,List<V>> toMapGrouping()
Entries are grouped based on the equality of the key.
This is a terminal operation.
public <A,R> ImmutableMap<K,R> toMapGrouping(Collector<? super V,A,R> valueCollector)
Entries are grouped based on the equality of the key. The collector allows the values to be flexibly combined.
This is a terminal operation.
A - the internal collector typeR - the type of the combined valuesvalueCollector - the collector used to combined the valuespublic ImmutableListMultimap<K,V> toListMultimap()
This is a terminal operation.
public ImmutableSetMultimap<K,V> toSetMultimap()
This is a terminal operation.
public void forEach(BiConsumer<? super K,? super V> action)
This is a terminal operation.
action - an action performed for each entry in the streampublic IntStream mapToInt(ToIntFunction<? super Map.Entry<K,V>> mapper)
public LongStream mapToLong(ToLongFunction<? super Map.Entry<K,V>> mapper)
public DoubleStream mapToDouble(ToDoubleFunction<? super Map.Entry<K,V>> mapper)
mapToDouble in interface Stream<Map.Entry<K,V>>public <R> Stream<R> flatMap(Function<? super Map.Entry<K,V>,? extends Stream<? extends R>> mapper)
public IntStream flatMapToInt(Function<? super Map.Entry<K,V>,? extends IntStream> mapper)
flatMapToInt in interface Stream<Map.Entry<K,V>>public LongStream flatMapToLong(Function<? super Map.Entry<K,V>,? extends LongStream> mapper)
flatMapToLong in interface Stream<Map.Entry<K,V>>public DoubleStream flatMapToDouble(Function<? super Map.Entry<K,V>,? extends DoubleStream> mapper)
flatMapToDouble in interface Stream<Map.Entry<K,V>>public void forEachOrdered(Consumer<? super Map.Entry<K,V>> action)
forEachOrdered in interface Stream<Map.Entry<K,V>>public <A> A[] toArray(IntFunction<A[]> generator)
public Map.Entry<K,V> reduce(Map.Entry<K,V> identity, BinaryOperator<Map.Entry<K,V>> accumulator)
public <U> U reduce(U identity,
BiFunction<U,? super Map.Entry<K,V>,U> accumulator,
BinaryOperator<U> combiner)
public <R> R collect(Supplier<R> supplier, BiConsumer<R,? super Map.Entry<K,V>> accumulator, BiConsumer<R,R> combiner)
public Spliterator<Map.Entry<K,V>> spliterator()
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>>>public MapStream<K,V> sequential()
sequential in interface BaseStream<Map.Entry<K,V>,Stream<Map.Entry<K,V>>>Copyright 2009-Present by OpenGamma Inc. and individual contributors
Apache v2 licensed
Additional documentation can be found at strata.opengamma.io.