/** * 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; }
Function<Entry<K, V>, K> keyMapper = Entry::getKey; Collector<Entry<K, V>, ?, D> mapping = Collectors.mapping(Entry::getValue, downstream); if (isParallel() && downstream.characteristics().contains(Characteristics.UNORDERED) && mapSupplier.get() instanceof ConcurrentMap) { return (M) collect(Collectors.groupingByConcurrent(keyMapper,
/** * 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; }
Function<Entry<K, V>, K> keyMapper = Entry::getKey; Collector<Entry<K, V>, ?, D> mapping = Collectors.mapping(Entry::getValue, downstream); if (isParallel() && downstream.characteristics().contains(Characteristics.UNORDERED)) { return collect(Collectors.groupingByConcurrent(keyMapper, mapping));