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:Main.java

public static <E> Collector<E, ImmutableList.Builder<E>, ImmutableList<E>> collectToImmutableList() {
    return Collector.of(
            ImmutableList::builder, ImmutableList.Builder::add, (ImmutableList.Builder<E> builder1,
                    ImmutableList.Builder<E> builder2) -> builder1.addAll(builder2.build()),
            ImmutableList.Builder::build, Characteristics.CONCURRENT);
}

From source file:com.ikanow.aleph2.shared.crud.mongodb.utils.MongoDbUtils.java

/** Creates a big $and/$or list of the list of "multi query components"
 * @param andVsOr - top level MongoDB operator
 * @param query_in - a multi query//  w w  w. j ava2s.  com
 * @return the MongoDB query object (no meta - that is added above)
 */
@SuppressWarnings("unchecked")
protected static <T> DBObject convertToMongoQuery_multi(final String andVsOr,
        final MultiQueryComponent<T> query_in) {

    return Patterns.match(query_in.getElements()).<DBObject>andReturn()
            .when(f -> f.isEmpty(), f -> new BasicDBObject())
            .otherwise(f -> f.stream().collect(Collector.of(BasicDBList::new, (acc, entry) -> {
                Patterns.match(entry).andAct()
                        .when(SingleQueryComponent.class,
                                e -> acc.add(convertToMongoQuery_single(getOperatorName(e.getOp()), e)))
                        .when(MultiQueryComponent.class,
                                e -> acc.add(convertToMongoQuery_multi(
                                        getOperatorName(((MultiQueryComponent<?>) e).getOp()),
                                        (MultiQueryComponent<?>) e)));
                //(at various other points in the code, oraclej has complained about this though ecj is happy, so just added these casts for safety)
            }, (a, b) -> {
                a.addAll(b);
                return a;
            }, acc -> (DBObject) new BasicDBObject(andVsOr, acc), Characteristics.UNORDERED)));
}

From source file:com.ikanow.aleph2.data_model.utils.TestCrudUtils.java

@SuppressWarnings("unchecked")
protected static <T> DBObject convertToMongoQuery_multi(final String andVsOr,
        final MultiQueryComponent<T> query_in) {

    return Patterns.match(query_in.getElements()).<DBObject>andReturn()
            .when(f -> f.isEmpty(), f -> new BasicDBObject())
            .otherwise(f -> f.stream().collect(Collector.of(BasicDBList::new, (acc, entry) -> {
                Patterns.match(entry).andAct()
                        .when(SingleQueryComponent.class,
                                e -> acc.add(convertToMongoQuery_single(getOperatorName(e.getOp()), e)))
                        .when(MultiQueryComponent.class,
                                e -> acc.add(convertToMongoQuery_multi(
                                        getOperatorName(((MultiQueryComponent<?>) e).getOp()),
                                        (MultiQueryComponent<?>) e)));
                //(at various other points in the code, oraclej has complained about this though ecj is happy, so just added these casts for safety)
            }, (a, b) -> {/*from w  w  w . ja  va 2  s  . c  o m*/
                a.addAll(b);
                return a;
            }, acc -> (DBObject) new BasicDBObject(andVsOr, acc), Characteristics.UNORDERED)));
}

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

/**
 * Collector used at the end of a stream to build an immutable set.
 * <p>//from  w  ww  .  jav  a2  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 ImmutableSet}.
 * @param <T> the type of element in the set
 * @return the immutable set collector
 */
public static <T> Collector<T, ImmutableSet.Builder<T>, ImmutableSet<T>> toImmutableSet() {
    return Collector.of(ImmutableSet.Builder<T>::new, ImmutableSet.Builder<T>::add,
            (l, r) -> l.addAll(r.build()), ImmutableSet.Builder<T>::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 sorted set.
 * <p>//w  w  w.j  a v  a  2  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 ImmutableSet}.
 * @param <T> the type of element in the sorted set
 * @return the immutable sorted set collector
 */
public static <T extends Comparable<?>> Collector<T, ImmutableSortedSet.Builder<T>, ImmutableSortedSet<T>> toImmutableSortedSet() {
    return Collector.of((Supplier<ImmutableSortedSet.Builder<T>>) ImmutableSortedSet::naturalOrder,
            ImmutableSortedSet.Builder<T>::add, (l, r) -> l.addAll(r.build()),
            ImmutableSortedSet.Builder<T>::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 sorted set.
 * <p>//from www.ja v a2 s .  co  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 ImmutableSet}.
 * @param <T> the type of element in the sorted set
 * @param comparator the comparator
 * @return the immutable sorted set collector
 */
public static <T> Collector<T, ImmutableSortedSet.Builder<T>, ImmutableSortedSet<T>> toImmutableSortedSet(
        Comparator<? super T> comparator) {
    return Collector.of(
            (Supplier<ImmutableSortedSet.Builder<T>>) () -> new ImmutableSortedSet.Builder<>(comparator),
            ImmutableSortedSet.Builder<T>::add, (l, r) -> l.addAll(r.build()),
            ImmutableSortedSet.Builder<T>::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 multiset.
 * <p>//  ww  w.j a  va  2s .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 ImmutableMultiset}.
 * @param <T> the type of element in the multiset
 * @return the immutable multiset collector
 */
public static <T> Collector<T, ImmutableMultiset.Builder<T>, ImmutableMultiset<T>> toImmutableMultiset() {
    return Collector.of(ImmutableMultiset.Builder<T>::new, ImmutableMultiset.Builder<T>::add,
            (l, r) -> l.addAll(r.build()), ImmutableMultiset.Builder<T>::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 map.
 * <p>//  w  ww  . j  a va2 s  . co 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 ImmutableMap}.
 * <p>
 * This returns a map by converting each stream element to a key and value.
 * The input stream must resolve to unique keys.
 * See {@link Collectors#toMap(Function, Function)} for more details.
 * @param <T> the type of the stream elements
 * @param <K> the type of the keys in the result map
 * @param <V> the type of the values in the result map
 * @param keyExtractor function to produce keys from stream elements
 * @param valueExtractor function to produce values from stream elements
 * @return the immutable map collector
 * @throws IllegalArgumentException if the same key is generated twice
 */
public static <T, K, V> Collector<T, ?, ImmutableMap<K, V>> toImmutableMap(
        Function<? super T, ? extends K> keyExtractor, Function<? super T, ? extends V> valueExtractor) {

    return Collector.of(ImmutableMap.Builder<K, V>::new,
            (builder, val) -> builder.put(keyExtractor.apply(val), valueExtractor.apply(val)),
            (l, r) -> l.putAll(r.build()), ImmutableMap.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 map.
 * <p>// w w w  .j a  v a 2 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 ImmutableMap}.
 * <p>
 * This returns a map by converting each stream element to a key and value.
 * If the same key is generated more than once the merge function is applied to the
 * values and the return value of the function is used as the value in the map.
 * @param <T> the type of the stream elements
 * @param <K> the type of the keys in the result map
 * @param <V> the type of the values in the result map
 * @param keyExtractor function to produce keys from stream elements
 * @param valueExtractor function to produce values from stream elements
 * @param mergeFn function to merge values with the same key
 * @return the immutable map collector
 */
public static <T, K, V> Collector<T, Map<K, V>, ImmutableMap<K, V>> toImmutableMap(
        Function<? super T, ? extends K> keyExtractor, Function<? super T, ? extends V> valueExtractor,
        BiFunction<? super V, ? super V, ? extends V> mergeFn) {

    return Collector.of(HashMap<K, V>::new,
            (map, val) -> map.merge(keyExtractor.apply(val), valueExtractor.apply(val), mergeFn),
            (m1, m2) -> mergeMaps(m1, m2, mergeFn), map -> ImmutableMap.copyOf(map),
            Collector.Characteristics.UNORDERED);
}

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

/**
 * Collector used at the end of a stream to build an immutable sorted map.
 * <p>/*ww  w . j  av  a  2 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 ImmutableSortedMap}.
 * <p>
 * This returns a map by converting each stream element to a key and value.
 * The input stream must resolve to unique keys.
 * See {@link Collectors#toMap(Function, Function)} for more details.
 * @param <T> the type of the stream elements
 * @param <K> the type of the keys in the result map
 * @param <V> the type of the values in the result map
 * @param keyExtractor function to produce keys from stream elements
 * @param valueExtractor function to produce values from stream elements
 * @return the immutable sorted map collector
 * @throws IllegalArgumentException if the same key is generated twice
 */
public static <T, K extends Comparable<?>, V> Collector<T, ?, ImmutableSortedMap<K, V>> toImmutableSortedMap(
        Function<? super T, ? extends K> keyExtractor, Function<? super T, ? extends V> valueExtractor) {

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