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

/**
 * Performs a transformation operation, mapping each element to a new value, adding it to the
 * {@code Vector.Builder} finishing it constructs a new {@code Vector}.
 *
 * @param supplier supply the vector builder
 * @param function the mapper/*from w  w w  .  jav  a2 s . c  o m*/
 * @param <T> the input type
 * @param <O> the output type
 * @return a transformation aggregator
 */
public static <T, O> Collector<T, ?, Vector> map(Supplier<Vector.Builder> supplier,
        Function<? super T, ? extends O> function) {
    return Collector.of(supplier, (acc, v) -> acc.add(function.apply(v)),
            (Vector.Builder left, Vector.Builder right) -> {
                left.addAll(right);
                return left;
            }, Vector.Builder::build);
}

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

/**
 * Returns an aggregator that filter values.
 *
 * @param supplier the vector builder//ww w. j a  va  2s  . c  o m
 * @param predicate the predicate. If {@code true} include value.
 * @param <T> the input type
 * @return a filtering aggregator
 */
public static <T> Collector<T, ?, Vector> filter(Supplier<Vector.Builder> supplier, Predicate<T> predicate) {
    return Collector.of(supplier, (acc, v) -> {
        if (predicate.test(v)) {
            acc.add(v);
        }
    }, (Vector.Builder left, Vector.Builder right) -> {
        left.addAll(right);
        return left;
    }, Vector.Builder::build);
}

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

/**
 * @param copies the number of copies of each element
 * @return an aggregator that repeats each value {@code copies} times.
 *//*  w ww . j av a 2s  .  c o m*/
public static <T> Collector<T, ?, Vector> each(Supplier<Vector.Builder> vb, int copies) {
    return Collector.of(vb, (acc, v) -> {
        for (int i = 0; i < copies; i++) {
            acc.add(v);
        }
    }, (Vector.Builder left, Vector.Builder right) -> {
        left.addAll(right);
        return left;
    }, Vector.Builder::build);
}

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

public static <T> Collector<T, ?, Vector> repeat(Supplier<Vector.Builder> vb, int copies) {
    return Collector.of(vb, Vector.Builder::add, (Vector.Builder left, Vector.Builder right) -> {
        left.addAll(right);/*  www  .j  a v  a 2 s.com*/
        return left;
    }, (v) -> {
        Vector temp = v.getView();
        int size = temp.size();
        for (int i = 1; i < copies; i++) {
            for (int j = 0; j < size; j++) {
                v.add(temp, j);
            }
        }
        return v.build();
    });
}

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

public static <T> Collector<T, ?, Vector> unique() {
    return Collector.of(HashSet::new, HashSet::add, (ts, ts2) -> {
        ts.addAll(ts2);//from   w ww. j av  a 2 s  . c o m
        return ts;
    }, ts -> new TypeInferenceVectorBuilder().addAll(ts).build());
}

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

public static <T> Collector<T, ?, Integer> nunique() {
    return Collector.of(HashSet::new, HashSet::add, (left, right) -> {
        left.addAll(right);/*w  ww .  j a  v  a2  s .c om*/
        return left;
    }, HashSet::size);
}

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

public static Collector<Object, ?, Vector> factorize() {
    class Factorize {

        Map<Object, Integer> map = new HashMap<>();
        Vector.Builder builder = Vector.Builder.of(Integer.class);
        int highest = 0;
    }//w w  w  . j  a  va  2s .c o  m

    // TODO: refactor into a real state-less collector
    Factorize factorize = new Factorize();
    return Collector.of(() -> factorize, (Factorize acc, Object value) -> {
        synchronized (factorize) {
            Integer code = acc.map.get(value);
            if (code == null) {
                code = acc.highest;
                acc.map.put(value, code);
                acc.highest++;
            }
            acc.builder.add(code);
        }
    }, (left, right) -> left, (acc) -> acc.builder.build());
}

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

public static Collector<Number, ?, StatisticalSummary> statisticalSummary() {
    return Collector.of(FastStatistics::new, (FastStatistics a, Number v) -> {
        if (!Is.NA(v)) {
            a.addValue(v.doubleValue());
        }/*from w ww  .  j  a  v a 2  s  .  c o  m*/
    }, (left, right) -> {
        throw new IllegalStateException("Can't collect statistics in parallel");
    }, FastStatistics::getSummary);
}

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

/**
 * @return an aggregator that computes the median.
 *//*from   w  ww  .  ja  v  a 2  s  .  c om*/
public static Collector<Number, ?, Double> median() {
    return Collector.of(ArrayList::new, ArrayList::add, (left, right) -> {
        left.addAll(right);
        return left;
    }, (ArrayList<Number> list) -> {
        int size = list.size();
        if (size == 0) {
            return Na.of(Double.class);
        } else if (size == 1) {
            return list.get(0).doubleValue();
        } else if (size == 2) {
            return (list.get(0).doubleValue() + list.get(1).doubleValue()) / 2;
        } else {
            list.sort((a, b) -> Double.compare(a.doubleValue(), b.doubleValue()));
            int index = (size - 1) / 2;
            if (size % 2 == 0) {
                return (list.get(index).doubleValue() + list.get(index + 1).doubleValue()) / 2;
            } else {
                return list.get(index).doubleValue();
            }
        }
    });
}

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

public static Collector<Number, ?, Number> max() {
    class MaxBox {

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

        void update(double v) {
            if (!Is.NA(v)) {
                hasValue = true;// ww w.j a v  a 2  s. c o  m
                value = Math.max(v, value);
            }
        }
    }

    return Collector.of(MaxBox::new, (a, v) -> a.update(v.doubleValue()), (left, right) -> {
        left.update(right.value);
        return left;
    }, (r) -> r.hasValue ? r.value : Na.DOUBLE);
}