/** * Performs an action for each key-value pair of this stream. * * <p> * This is a <a href="package-summary.html#StreamOps">terminal</a> * operation. * * <p> * The behavior of this operation is explicitly nondeterministic. For * parallel stream pipelines, this operation does <em>not</em> 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. * * @param action a non-interfering action to perform on the key and value * @see #forEach(java.util.function.Consumer) */ public void forKeyValue(BiConsumer<? super K, ? super V> action) { forEach(toConsumer(action)); }
/** * Returns a {@link NavigableMap} containing the elements of this stream. * There are no guarantees on the type or serializability of the * {@code NavigableMap} returned; if more control over the returned * {@code Map} is required, use {@link #toCustomMap(Supplier)}. * * <p> * This is a <a href="package-summary.html#StreamOps">terminal</a> * operation. * * <p> * Returned {@code NavigableMap} is guaranteed to be modifiable. * * <p> * For parallel stream the concurrent {@code NavigableMap} is created. * * @return a {@code NavigableMap} containing the elements of this stream * @see Collectors#toMap(Function, Function) * @see Collectors#toConcurrentMap(Function, Function) * @throws IllegalStateException if this stream contains duplicate keys * (according to {@link Object#equals(Object)}) * @since 0.6.5 */ public NavigableMap<K, V> toNavigableMap() { NavigableMap<K, V> map = isParallel() ? new ConcurrentSkipListMap<>() : new TreeMap<>(); forEach(toMapConsumer(map)); return map; }
/** * Returns a {@link Map} containing the elements of this stream. There are * no guarantees on the type or serializability of the {@code Map} returned; * if more control over the returned {@code Map} is required, use * {@link #toCustomMap(Supplier)}. * * <p> * This is a <a href="package-summary.html#StreamOps">terminal</a> * operation. * * <p> * Returned {@code Map} is guaranteed to be modifiable. * * <p> * For parallel stream the concurrent {@code Map} is created. * * @return a {@code Map} containing the elements of this stream * @throws IllegalStateException if this stream contains duplicate keys * (according to {@link Object#equals(Object)}) * @see Collectors#toMap(Function, Function) * @see Collectors#toConcurrentMap(Function, Function) * @see #toImmutableMap() */ public Map<K, V> toMap() { Map<K, V> map = isParallel() ? new ConcurrentHashMap<>() : new HashMap<>(); forEach(toMapConsumer(map)); return map; }
/** * Returns a {@link Map} containing the elements of this stream. The * {@code Map} is created by a provided supplier function. * * <p> * This is a <a href="package-summary.html#StreamOps">terminal</a> * operation. * * @param <M> the type of the resulting map * @param mapSupplier a function which returns a new, empty {@code Map} into * which the results will be inserted * @return a {@code Map} containing the elements of this stream * @throws IllegalStateException if this stream contains duplicate keys * (according to {@link Object#equals(Object)}) * @see Collectors#toMap(Function, Function) * @see Collectors#toConcurrentMap(Function, Function) */ public <M extends Map<K, V>> M toCustomMap(Supplier<M> mapSupplier) { M map = mapSupplier.get(); if (isParallel() && !(map instanceof ConcurrentMap)) { return collect(mapSupplier, (m, t) -> addToMap(m, t.getKey(), Objects.requireNonNull(t.getValue())), (m1, m2) -> m2.forEach((k, v) -> addToMap(m1, k, v))); } forEach(toMapConsumer(map)); return map; }
/** * Drains the stream content into the supplied {@code Map}. * * <p> * This is a <a href="package-summary.html#StreamOps">terminal</a> * operation. * * @param <M> type of the resulting map * @param map a mutable map to put the stream elements into * @return the supplied map, updated from this stream * @throws IllegalStateException if this stream contains duplicate keys, or * the stream contains the key which was already present in the map * (according to {@link Object#equals(Object)}) * @since 0.6.3 */ public <M extends Map<K, V>> M into(M map) { Consumer<? super Entry<K, V>> cons = toMapConsumer(map); if (isParallel()) { if (map instanceof ConcurrentMap) forEach(cons); else toMap().entrySet().forEach(cons); } else { spliterator().forEachRemaining(cons); } return map; }