Client and backend. This class is shared: the same code is compiled into your app and into your server, so everything on this page works in both.

public final class Collectors

  1. Object
  2. Collectors

The common Collectors: into collections, maps, strings, groups and numbers.

The results are the JDK’s: groupingBy answers a HashMap of ArrayLists unless told otherwise, toMap refuses a second value for a key unless given a function to merge them, and the unmodifiable variants refuse null.

Every function here is a class of its own, made only by the method that needs it, so an application carries the collectors it uses and no others.

Methods

public static <T, C extends Collection<T>> Collector<T, ?, C> toCollection(Supplier<C> collectionFactory)
public static <T> Collector<T, ?, List<T>> toList()
public static <T> Collector<T, ?, List<T>> toUnmodifiableList()
public static <T> Collector<T, ?, Set<T>> toSet()
public static <T> Collector<T, ?, Set<T>> toUnmodifiableSet()
public static Collector<CharSequence, ?, String> joining()
public static Collector<CharSequence, ?, String> joining(CharSequence delimiter)
public static Collector<CharSequence, ?, String> joining(CharSequence delimiter, CharSequence prefix, CharSequence suffix)
public static <T, U, A, R> Collector<T, ?, R> mapping(Function<? super T, ? extends U> mapper, Collector<? super U, A, R> downstream)
public static <T, A, R> Collector<T, ?, R> filtering(Predicate<? super T> predicate, Collector<? super T, A, R> downstream)
public static <T, U, A, R> Collector<T, ?, R> flatMapping(Function<? super T, ? extends Stream<? extends U>> mapper, Collector<? super U, A, R> downstream)
public static <T, A, R, RR> Collector<T, A, RR> collectingAndThen(Collector<T, A, R> downstream, Function<R, RR> finisher)
public static <T> Collector<T, ?, Long> counting()
public static <T> Collector<T, ?, Integer> summingInt(ToIntFunction<? super T> mapper)
public static <T> Collector<T, ?, Long> summingLong(ToLongFunction<? super T> mapper)
public static <T> Collector<T, ?, Double> summingDouble(ToDoubleFunction<? super T> mapper)
public static <T> Collector<T, ?, Double> averagingInt(ToIntFunction<? super T> mapper)
public static <T> Collector<T, ?, Double> averagingLong(ToLongFunction<? super T> mapper)
public static <T> Collector<T, ?, Double> averagingDouble(ToDoubleFunction<? super T> mapper)
public static <T> Collector<T, ?, IntSummaryStatistics> summarizingInt(ToIntFunction<? super T> mapper)
public static <T> Collector<T, ?, LongSummaryStatistics> summarizingLong(ToLongFunction<? super T> mapper)
public static <T> Collector<T, ?, DoubleSummaryStatistics> summarizingDouble(ToDoubleFunction<? super T> mapper)
public static <T> Collector<T, ?, T> reducing(T identity, BinaryOperator<T> op)
public static <T> Collector<T, ?, Optional<T>> reducing(BinaryOperator<T> op)
public static <T, U> Collector<T, ?, U> reducing(U identity, Function<? super T, ? extends U> mapper, BinaryOperator<U> op)
public static <T> Collector<T, ?, Optional<T>> minBy(Comparator<? super T> comparator)
public static <T> Collector<T, ?, Optional<T>> maxBy(Comparator<? super T> comparator)
public static <T, K> Collector<T, ?, Map<K, List<T>>> groupingBy(Function<? super T, ? extends K> classifier)
public static <T, K, A, D> Collector<T, ?, Map<K, D>> groupingBy(Function<? super T, ? extends K> classifier, Collector<? super T, A, D> downstream)
public static <T, K, D, A, M extends Map<K, D>> Collector<T, ?, M> groupingBy(Function<? super T, ? extends K> classifier, Supplier<M> mapFactory, Collector<? super T, A, D> downstream)
public static <T> Collector<T, ?, Map<Boolean, List<T>>> partitioningBy(Predicate<? super T> predicate)
public static <T, D, A> Collector<T, ?, Map<Boolean, D>> partitioningBy(Predicate<? super T> predicate, Collector<? super T, A, D> downstream)
public static <T, K, U> Collector<T, ?, Map<K, U>> toMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper)
public static <T, K, U> Collector<T, ?, Map<K, U>> toMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper, BinaryOperator<U> mergeFunction)
public static <T, K, U, M extends Map<K, U>> Collector<T, ?, M> toMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper, BinaryOperator<U> mergeFunction, Supplier<M> mapFactory)
public static <T, K, U> Collector<T, ?, Map<K, U>> toUnmodifiableMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper)
public static <T, K, U> Collector<T, ?, Map<K, U>> toUnmodifiableMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper, BinaryOperator<U> mergeFunction)

Inherited methods

Method details

toCollection

public static <T, C extends Collection<T>> Collector<T, ?, C> toCollection(Supplier<C> collectionFactory)

toList

public static <T> Collector<T, ?, List<T>> toList()

toUnmodifiableList

public static <T> Collector<T, ?, List<T>> toUnmodifiableList()

toSet

public static <T> Collector<T, ?, Set<T>> toSet()

toUnmodifiableSet

public static <T> Collector<T, ?, Set<T>> toUnmodifiableSet()

joining

public static Collector<CharSequence, ?, String> joining()

joining

public static Collector<CharSequence, ?, String> joining(CharSequence delimiter)

joining

public static Collector<CharSequence, ?, String> joining(CharSequence delimiter, CharSequence prefix, CharSequence suffix)

mapping

public static <T, U, A, R> Collector<T, ?, R> mapping(Function<? super T, ? extends U> mapper, Collector<? super U, A, R> downstream)

filtering

public static <T, A, R> Collector<T, ?, R> filtering(Predicate<? super T> predicate, Collector<? super T, A, R> downstream)

flatMapping

public static <T, U, A, R> Collector<T, ?, R> flatMapping(Function<? super T, ? extends Stream<? extends U>> mapper, Collector<? super U, A, R> downstream)

collectingAndThen

public static <T, A, R, RR> Collector<T, A, RR> collectingAndThen(Collector<T, A, R> downstream, Function<R, RR> finisher)

counting

public static <T> Collector<T, ?, Long> counting()

summingInt

public static <T> Collector<T, ?, Integer> summingInt(ToIntFunction<? super T> mapper)

summingLong

public static <T> Collector<T, ?, Long> summingLong(ToLongFunction<? super T> mapper)

summingDouble

public static <T> Collector<T, ?, Double> summingDouble(ToDoubleFunction<? super T> mapper)

averagingInt

public static <T> Collector<T, ?, Double> averagingInt(ToIntFunction<? super T> mapper)

averagingLong

public static <T> Collector<T, ?, Double> averagingLong(ToLongFunction<? super T> mapper)

averagingDouble

public static <T> Collector<T, ?, Double> averagingDouble(ToDoubleFunction<? super T> mapper)

summarizingInt

public static <T> Collector<T, ?, IntSummaryStatistics> summarizingInt(ToIntFunction<? super T> mapper)

summarizingLong

public static <T> Collector<T, ?, LongSummaryStatistics> summarizingLong(ToLongFunction<? super T> mapper)

summarizingDouble

public static <T> Collector<T, ?, DoubleSummaryStatistics> summarizingDouble(ToDoubleFunction<? super T> mapper)

reducing

public static <T> Collector<T, ?, T> reducing(T identity, BinaryOperator<T> op)

reducing

public static <T> Collector<T, ?, Optional<T>> reducing(BinaryOperator<T> op)

reducing

public static <T, U> Collector<T, ?, U> reducing(U identity, Function<? super T, ? extends U> mapper, BinaryOperator<U> op)

minBy

public static <T> Collector<T, ?, Optional<T>> minBy(Comparator<? super T> comparator)

maxBy

public static <T> Collector<T, ?, Optional<T>> maxBy(Comparator<? super T> comparator)

groupingBy

public static <T, K> Collector<T, ?, Map<K, List<T>>> groupingBy(Function<? super T, ? extends K> classifier)

groupingBy

public static <T, K, A, D> Collector<T, ?, Map<K, D>> groupingBy(Function<? super T, ? extends K> classifier, Collector<? super T, A, D> downstream)

groupingBy

public static <T, K, D, A, M extends Map<K, D>> Collector<T, ?, M> groupingBy(Function<? super T, ? extends K> classifier, Supplier<M> mapFactory, Collector<? super T, A, D> downstream)

partitioningBy

public static <T> Collector<T, ?, Map<Boolean, List<T>>> partitioningBy(Predicate<? super T> predicate)

partitioningBy

public static <T, D, A> Collector<T, ?, Map<Boolean, D>> partitioningBy(Predicate<? super T> predicate, Collector<? super T, A, D> downstream)

toMap

public static <T, K, U> Collector<T, ?, Map<K, U>> toMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper)

toMap

public static <T, K, U> Collector<T, ?, Map<K, U>> toMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper, BinaryOperator<U> mergeFunction)

toMap

public static <T, K, U, M extends Map<K, U>> Collector<T, ?, M> toMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper, BinaryOperator<U> mergeFunction, Supplier<M> mapFactory)

toUnmodifiableMap

public static <T, K, U> Collector<T, ?, Map<K, U>> toUnmodifiableMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper)

toUnmodifiableMap

public static <T, K, U> Collector<T, ?, Map<K, U>> toUnmodifiableMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper, BinaryOperator<U> mergeFunction)