/** * Returns a {@link Map} containing the elements of this stream. The * {@code Map} is created by a provided supplier function. * * <p> * If the mapped keys contains duplicates (according to * {@link Object#equals(Object)}), the value mapping function is applied to * each equal element, and the results are merged using the provided merging * function. * * <p> * This is a <a href="package-summary.html#StreamOps">terminal</a> * operation. * * @param <M> the type of the resulting map * @param mergeFunction a merge function, used to resolve collisions between * values associated with the same key. * @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 * @see Collectors#toMap(Function, Function) * @see Collectors#toConcurrentMap(Function, Function) */ public <M extends Map<K, V>> M toCustomMap(BinaryOperator<V> mergeFunction, Supplier<M> mapSupplier) { Function<Entry<K, V>, K> keyMapper = Entry::getKey; Function<Entry<K, V>, V> valueMapper = Entry::getValue; return collect(Collectors.toMap(keyMapper, valueMapper, mergeFunction, mapSupplier)); }
/** * 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(BinaryOperator, Supplier)}. * * <p> * If the mapped keys contains duplicates (according to * {@link Object#equals(Object)}), the value mapping function is applied to * each equal element, and the results are merged using the provided merging * function. * * <p> * This is a <a href="package-summary.html#StreamOps">terminal</a> * operation. * * <p> * Returned {@code NavigableMap} is guaranteed to be modifiable. * * @param mergeFunction a merge function, used to resolve collisions between * values associated with the same key, as supplied to * {@link Map#merge(Object, Object, BiFunction)} * @return a {@code NavigableMap} containing the elements of this stream * @see Collectors#toMap(Function, Function) * @since 0.6.5 */ public NavigableMap<K, V> toNavigableMap(BinaryOperator<V> mergeFunction) { return collect(Collectors.toMap(Entry::getKey, Entry::getValue, mergeFunction, TreeMap::new)); }
Function<Entry<K, V>, K> keyMapper = Entry::getKey; Function<Entry<K, V>, V> valueMapper = Entry::getValue; return collect(Collectors.toMap(keyMapper, valueMapper, mergeFunction, HashMap::new));
Collector<Entry<K, V>, ?, D> mapping = Collectors.mapping(Entry::getValue, downstream); if (isParallel() && downstream.characteristics().contains(Characteristics.UNORDERED)) { return collect(Collectors.groupingByConcurrent(keyMapper, mapping)); return collect(Collectors.groupingBy(keyMapper, mapping));
if (isParallel() && downstream.characteristics().contains(Characteristics.UNORDERED) && mapSupplier.get() instanceof ConcurrentMap) { return (M) collect(Collectors.groupingByConcurrent(keyMapper, (Supplier<? extends ConcurrentMap<K, D>>) mapSupplier, mapping)); return collect(Collectors.groupingBy(keyMapper, mapSupplier, mapping));
/** * 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; }