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, R> Collector<T, R, R> of(Supplier<R> supplier, BiConsumer<R, T> accumulator,
        BinaryOperator<R> combiner, Characteristics... characteristics) 

Source Link

Document

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

Usage

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

public static Collector<Number, ?, Number> min() {
    class MinBox {

        double value = Double.NEGATIVE_INFINITY;
        boolean hasValue = false;

        void update(double v) {
            if (!Is.NA(v)) {
                hasValue = true;//from  www.j av a 2s. c o m
                value = Math.min(v, value);
            }
        }
    }
    return Collector.of(MinBox::new, (a, v) -> a.update(v.doubleValue()), (left, right) -> {
        left.update(right.value);
        return left;
    }, (r) -> r.hasValue ? r.value : Na.DOUBLE);
}

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

/**
 * Returns a collector that counts non-NA values
 *
 * @return a collector that counts non-NA values
 *///from w  w w  .  ja  v a  2  s .  c  om
public static Collector<Object, ?, Integer> count() {
    return Collector.of(() -> new int[1], (int[] a, Object b) -> {
        if (!Is.NA(b)) {
            a[0] += 1;
        }
    }, (int[] left, int[] right) -> {
        left[0] += right[0];
        return left;
    }, (int[] a) -> a[0]);
}

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

/**
 * Returns a collector that collects objects into a vector while filling NA-values with the
 * supplied value//from  www .j  av a  2s.c  o m
 *
 * @param fill the value to fill NA with
 * @return a collector for filling NA values
 */
public static Collector<Object, ?, Vector> fillNa(Object fill) {
    return Collector.of(TypeInferenceVectorBuilder::new, (builder, t) -> {
        if (Is.NA(t)) {
            builder.add(fill);
        } else {
            builder.add(t);
        }
    }, (left, right) -> {
        left.addAll(right);
        return left;
    }, Vector.Builder::build);
}

From source file:org.briljantframework.data.vector.Vectors.java

public static <T, V extends Vector.Builder> Collector<T, ?, Vector> collector(Supplier<V> supplier) {
    return Collector.of(supplier, Vector.Builder::add, (left, right) -> {
        left.addAll(right.getView());/*from   ww  w .  j  av  a  2 s.  com*/
        return left;
    }, Vector.Builder::build);
}

From source file:org.trellisldp.api.TrellisUtils.java

/**
 * Collect a stream of Triples into a Graph.
 *
 * @return a graph/*from   w w w .j av  a  2  s.com*/
 */
public static Collector<Triple, ?, Graph> toGraph() {
    return Collector.of(rdf::createGraph, Graph::add, (left, right) -> {
        right.iterate().forEach(left::add);
        return left;
    }, UNORDERED);
}