return new CollectorImpl<>(downstream.supplier(), downstream.accumulator(), downstream.combiner(),
BiConsumer<M, T> accumulator = (map, element) -> ConcurrentMaps.merge(map, keyMapper.apply(element), valueMapper.apply(element), mergeFunction); return new CollectorImpl<>(mapFactory, accumulator, mapMergerConcurrent(mergeFunction), CH_CONCURRENT_ID);
return new CollectorImpl<>( () -> new double[4], (a, t) -> { double val = mapper.applyAsDouble(t); sumWithCompensation(a, val); a[2]++; a[3]+= val;},
Collector<? super U, A, R> downstream) { BiConsumer<A, ? super U> downstreamAccumulator = downstream.accumulator(); return new CollectorImpl<>(downstream.supplier(), (r, t) -> downstreamAccumulator.accept(r, mapper.apply(t)), downstream.combiner(), downstream.finisher(),
Collector<? super U, A, R> downstream) { BiConsumer<A, ? super U> downstreamAccumulator = downstream.accumulator(); return new CollectorImpl<>(downstream.supplier(), (r, t) -> downstreamAccumulator.accept(r, mapper.apply(t)), downstream.combiner(), downstream.finisher(),
cs = Collections.unmodifiableSet(cs); return new Collectors.CollectorImpl<>(supplier, accumulator, combiner, finisher, cs);
return new CollectorImpl<>( () -> new double[3], (a, t) -> { double val = mapper.applyAsDouble(t);
: Collections.unmodifiableSet(EnumSet.of(Collector.Characteristics.IDENTITY_FINISH, characteristics)); return new Collectors.CollectorImpl<>(supplier, accumulator, combiner, cs);
cs = Collections.unmodifiableSet(cs); return new Collectors.CollectorImpl<>(supplier, accumulator, combiner, finisher, cs);
/** * Returns a {@code Collector} that accumulates the input elements into an * {@link Sets#of(Object[]) unmodifiable Set}. The returned * Collector disallows null values and will throw {@code NullPointerException} * if it is presented with a null value. If the input contains duplicate elements, * an arbitrary element of the duplicates is preserved. * * <p>This is an {@link Collector.Characteristics#UNORDERED unordered} * Collector. * * @param <T> the type of the input elements * @return a {@code Collector} which collects all the input elements into an * <a href="../Sets.html#unmodifiable">unmodifiable Set</a> * @since 10 */ @SuppressWarnings("unchecked") public static <T> Collector<T, ?, Set<T>> toUnmodifiableSet() { return new CollectorImpl<>(hashSetNew(), setAdd(), (left, right) -> { if (left.size() < right.size()) { right.addAll(left); return right; } else { left.addAll(right); return left; } }, set -> (Set<T>) Sets.of(set.toArray()), CH_UNORDERED_NOID); }
/** * Returns a {@code Collector} which performs a reduction of its * input elements under a specified {@code BinaryOperator} using the * provided identity. * * <p><b>API Note:</b><br> * The {@code reducing()} collectors are most useful when used in a * multi-level reduction, downstream of {@code groupingBy} or * {@code partitioningBy}. To perform a simple reduction on a stream, * use {@link Stream#reduce(Object, BinaryOperator)}} instead. * * @param <T> element type for the input and output of the reduction * @param identity the identity value for the reduction (also, the value * that is returned when there are no input elements) * @param op a {@code BinaryOperator<T>} used to reduce the input elements * @return a {@code Collector} which implements the reduction operation * * @see #reducing(BinaryOperator) * @see #reducing(Object, Function, BinaryOperator) */ public static <T> Collector<T, ?, T> reducing(T identity, BinaryOperator<T> op) { return new CollectorImpl<>( boxSupplier(identity), (a, t) -> { a[0] = op.apply(a[0], t); }, (a, b) -> { a[0] = op.apply(a[0], b[0]); return a; }, a -> a[0], CH_NOID); }
/** * Returns a {@code Collector} which performs a reduction of its * input elements under a specified {@code BinaryOperator} using the * provided identity. * * <p><b>API Note:</b><br> * The {@code reducing()} collectors are most useful when used in a * multi-level reduction, downstream of {@code groupingBy} or * {@code partitioningBy}. To perform a simple reduction on a stream, * use {@link Stream#reduce(Object, BinaryOperator)}} instead. * * @param <T> element type for the input and output of the reduction * @param identity the identity value for the reduction (also, the value * that is returned when there are no input elements) * @param op a {@code BinaryOperator<T>} used to reduce the input elements * @return a {@code Collector} which implements the reduction operation * * @see #reducing(BinaryOperator) * @see #reducing(Object, Function, BinaryOperator) */ public static <T> Collector<T, ?, T> reducing(T identity, BinaryOperator<T> op) { return new CollectorImpl<>( boxSupplier(identity), (a, t) -> { a[0] = op.apply(a[0], t); }, (a, b) -> { a[0] = op.apply(a[0], b[0]); return a; }, a -> a[0], CH_NOID); }
/** * Returns a {@code Collector} that accumulates the input elements into an * {@link Sets#of(Object[]) unmodifiable Set}. The returned * Collector disallows null values and will throw {@code NullPointerException} * if it is presented with a null value. If the input contains duplicate elements, * an arbitrary element of the duplicates is preserved. * * <p>This is an {@link Collector.Characteristics#UNORDERED unordered} * Collector. * * @param <T> the type of the input elements * @return a {@code Collector} which collects all the input elements into an * <a href="../Sets.html#unmodifiable">unmodifiable Set</a> * @since 10 */ @SuppressWarnings("unchecked") public static <T> Collector<T, ?, Set<T>> toUnmodifiableSet() { return new CollectorImpl<>(hashSetNew(), setAdd(), (left, right) -> { if (left.size() < right.size()) { right.addAll(left); return right; } else { left.addAll(right); return left; } }, set -> (Set<T>) Sets.of(set.toArray()), CH_UNORDERED_NOID); }
: Collections.unmodifiableSet(EnumSet.of(Collector.Characteristics.IDENTITY_FINISH, characteristics)); return new Collectors.CollectorImpl<>(supplier, accumulator, combiner, cs);
/** * Returns a {@code Collector} that accumulates the input elements into a * new {@code Set}. There are no guarantees on the type, mutability, * serializability, or thread-safety of the {@code Set} returned; if more * control over the returned {@code Set} is required, use * {@link #toCollection(Supplier)}. * * <p>This is an {@link Collector.Characteristics#UNORDERED unordered} * Collector. * * @param <T> the type of the input elements * @return a {@code Collector} which collects all the input elements into a * {@code Set} */ public static <T> Collector<T, ?, Set<T>> toSet() { return new CollectorImpl<>(hashSetNew(), setAdd(), (left, right) -> { if (left.size() < right.size()) { right.addAll(left); return right; } else { left.addAll(right); return left; } }, CH_UNORDERED_ID); }
/** * Returns a {@code Collector} that accumulates the input elements into a * new {@code Set}. There are no guarantees on the type, mutability, * serializability, or thread-safety of the {@code Set} returned; if more * control over the returned {@code Set} is required, use * {@link #toCollection(Supplier)}. * * <p>This is an {@link Collector.Characteristics#UNORDERED unordered} * Collector. * * @param <T> the type of the input elements * @return a {@code Collector} which collects all the input elements into a * {@code Set} */ public static <T> Collector<T, ?, Set<T>> toSet() { return new CollectorImpl<>(hashSetNew(), setAdd(), (left, right) -> { if (left.size() < right.size()) { right.addAll(left); return right; } else { left.addAll(right); return left; } }, CH_UNORDERED_ID); }
/** * Returns a {@code Collector} that concatenates the input elements, * separated by the specified delimiter, with the specified prefix and * suffix, in encounter order. * * @param delimiter the delimiter to be used between each element * @param prefix the sequence of characters to be used at the beginning * of the joined result * @param suffix the sequence of characters to be used at the end * of the joined result * @return A {@code Collector} which concatenates CharSequence elements, * separated by the specified delimiter, in encounter order */ public static Collector<CharSequence, ?, String> joining(CharSequence delimiter, CharSequence prefix, CharSequence suffix) { return new CollectorImpl<>( () -> new StringJoiner(delimiter, prefix, suffix), StringJoiner::add, StringJoiner::merge, StringJoiner::toString, CH_NOID); }
/** * Returns a {@code Collector} which applies an {@code double}-producing * mapping function to each input element, and returns summary statistics * for the resulting values. * * @param <T> the type of the input elements * @param mapper a mapping function to apply to each element * @return a {@code Collector} implementing the summary-statistics reduction * * @see #summarizingLong(ToLongFunction) * @see #summarizingInt(ToIntFunction) */ public static <T> Collector<T, ?, DoubleSummaryStatistics> summarizingDouble(ToDoubleFunction<? super T> mapper) { return new CollectorImpl<T, DoubleSummaryStatistics, DoubleSummaryStatistics>( DBL_SUM_STATS, (r, t) -> r.accept(mapper.applyAsDouble(t)), (l, r) -> { l.combine(r); return l; }, CH_ID); }
/** * Returns a {@code Collector} that concatenates the input elements, * separated by the specified delimiter, with the specified prefix and * suffix, in encounter order. * * @param delimiter the delimiter to be used between each element * @param prefix the sequence of characters to be used at the beginning * of the joined result * @param suffix the sequence of characters to be used at the end * of the joined result * @return A {@code Collector} which concatenates CharSequence elements, * separated by the specified delimiter, in encounter order */ public static Collector<CharSequence, ?, String> joining(CharSequence delimiter, CharSequence prefix, CharSequence suffix) { return new CollectorImpl<>( () -> new StringJoiner(delimiter, prefix, suffix), StringJoiner::add, StringJoiner::merge, StringJoiner::toString, CH_NOID); }
/** * Returns a {@code Collector} that accumulates the input elements into an * {@link Lists#of(Object[]) unmodifiable List} in encounter * order. The returned Collector disallows null values and will throw * {@code NullPointerException} if it is presented with a null value. * * @param <T> the type of the input elements * @return a {@code Collector} which collects all the input elements into an * <a href="../Lists.html#unmodifiable">unmodifiable List</a>, in encounter order * @since 10 */ @SuppressWarnings("unchecked") public static <T> Collector<T, ?, List<T>> toUnmodifiableList() { return new CollectorImpl<>(arrayListNew(), listAdd(), (left, right) -> { left.addAll(right); return left; }, list -> (List<T>) Lists.of(list.toArray()), CH_NOID); }