Example usage for java.util.stream Collector of

List of usage examples for java.util.stream Collector of

Introduction

In this page you can find the example usage for java.util.stream Collector of.

Prototype

public static <T, A, R> Collector<T, A, R> of(Supplier<A> supplier, BiConsumer<A, T> accumulator,
        BinaryOperator<A> combiner, Function<A, R> finisher, Characteristics... characteristics) 

Source Link

Document

Returns a new Collector described by the given supplier , accumulator , combiner , and finisher functions.

Usage

From source file:com.github.steveash.guavate.Guavate.java

/**
 * Collector used at the end of a stream to build an immutable multimap.
 * <p>//from w w w  .  j  a  va2 s . com
 * A collector is used to gather data at the end of a stream operation.
 * This method returns a collector allowing streams to be gathered into
 * an {@link ImmutableListMultimap}.
 * <p>
 * This returns a multimap by converting each stream element to a key and value.
 * Stream elements may be converted to the same key, with the values forming a multimap list.
 * @param <T> the type of the stream elements
 * @param <K> the type of the keys in the result multimap
 * @param <V> the type of the values in the result multimap
 * @param keyExtractor function to produce keys from stream elements
 * @param valueExtractor function to produce values from stream elements
 * @return the immutable multimap collector
 */
public static <T, K, V> Collector<T, ?, ImmutableListMultimap<K, V>> toImmutableListMultimap(
        Function<? super T, ? extends K> keyExtractor, Function<? super T, ? extends V> valueExtractor) {

    return Collector.of(ImmutableListMultimap.Builder<K, V>::new,
            (builder, val) -> builder.put(keyExtractor.apply(val), valueExtractor.apply(val)),
            (l, r) -> l.putAll(r.build()), ImmutableListMultimap.Builder<K, V>::build,
            Collector.Characteristics.UNORDERED);
}

From source file:com.github.steveash.guavate.Guavate.java

/**
 * Collector used at the end of a stream to build an immutable multimap.
 * <p>/*from   ww  w.  ja  va2 s .c o m*/
 * A collector is used to gather data at the end of a stream operation.
 * This method returns a collector allowing streams to be gathered into
 * an {@link ImmutableSetMultimap}.
 * <p>
 * This returns a multimap by converting each stream element to a key and value.
 * Stream elements may be converted to the same key, with the values forming a multimap set.
 * @param <T> the type of the stream elements
 * @param <K> the type of the keys in the result multimap
 * @param <V> the type of the values in the result multimap
 * @param keyExtractor function to produce keys from stream elements
 * @param valueExtractor function to produce values from stream elements
 * @return the immutable multimap collector
 */
public static <T, K, V> Collector<T, ?, ImmutableSetMultimap<K, V>> toImmutableSetMultimap(
        Function<? super T, ? extends K> keyExtractor, Function<? super T, ? extends V> valueExtractor) {

    return Collector.of(ImmutableSetMultimap.Builder<K, V>::new,
            (builder, val) -> builder.put(keyExtractor.apply(val), valueExtractor.apply(val)),
            (l, r) -> l.putAll(r.build()), ImmutableSetMultimap.Builder<K, V>::build,
            Collector.Characteristics.UNORDERED);
}

From source file:org.briljantframework.data.Collectors.java

public static <T> Collector<T, ?, Vector> valueCounts() {
    return Collector.of(HashMap::new, (map, t) -> map.compute(t, (v, c) -> c == null ? 1 : c + 1),
            new BinaryOperator<HashMap<T, Integer>>() {
                @Override//from   www .j  ava2s  . c om
                public HashMap<T, Integer> apply(HashMap<T, Integer> left, HashMap<T, Integer> right) {
                    right.forEach((k, v) -> left.merge(k, v, (Integer o, Integer n) -> o == null ? n : o + n));
                    return left;
                }
            }, (map) -> {
                Vector.Builder b = new TypeInferenceVectorBuilder();
                for (Map.Entry<T, Integer> e : map.entrySet()) {
                    b.set(e.getKey(), e.getValue());
                }
                return b.build();
            }, Collector.Characteristics.UNORDERED);
}

From source file:org.briljantframework.data.Collectors.java

public static <T> Collector<T, ?, T> mode() {
    return Collector.of(HashMap::new, (HashMap<T, Integer> map, T value) -> map.compute(value,
            (key, count) -> count == null ? 1 : count + 1), (left, right) -> {
                right.forEach((k, v) -> left.merge(k, v, (Integer o, Integer n) -> o == null ? n : o + n));
                return left;
            }, (HashMap<T, Integer> map) -> {
                int max = 0;
                T value = null;/*from w w w  . jav  a2 s.c o  m*/
                for (Map.Entry<T, Integer> k : map.entrySet()) {
                    if (k.getValue() > max) {
                        value = k.getKey();
                    }
                }
                return value;
            }, Collector.Characteristics.UNORDERED);
}

From source file:org.briljantframework.data.Collectors.java

public static <T, A, R, F> Collector<T, ?, F> withFinisher(Collector<T, A, R> collector,
        Function<R, F> finisher) {
    Function<A, R> f = collector.finisher();

    Set<Collector.Characteristics> characteristics = collector.characteristics();
    Collector.Characteristics[] empty = new Collector.Characteristics[characteristics.size()];
    return Collector.of(collector.supplier(), collector.accumulator(), collector.combiner(),
            f.andThen(finisher), characteristics.toArray(empty));
}

From source file:org.decampo.examples.collections.MoreCollectors.java

public static <T, K, V> Collector<T, SetValuedMap<K, V>, SetValuedMap<K, V>> toSetValuedMap(
        Function<T, K> keyMapper, Function<T, V> valueMapper) {

    return Collector.of(HashSetValuedHashMap::new,
            (map, t) -> map.put(keyMapper.apply(t), valueMapper.apply(t)), (map1, map2) -> {
                map1.putAll(map2);/*  ww w  . j a  v a  2  s.  c o m*/
                return map1;
            }, Collector.Characteristics.IDENTITY_FINISH, Collector.Characteristics.UNORDERED);
}