use of java.util.function.BinaryOperator in project sandbox by backpaper0.
the class MapReduceExample method main.
public static void main(final String[] args) {
final PrintWriter out = new PrintWriter(System.out);
final List<String> list = Arrays.asList("foo", "bar", "baz", "hoge", "foobar");
final Function<String, Integer> mapper = s -> {
out.printf("mapper: %s%n", Thread.currentThread().getName());
return s.length();
};
final BinaryOperator<Integer> reducer = (a, b) -> {
out.printf("reducer: %s%n", Thread.currentThread().getName());
return a + b;
};
final Integer result = MapReducer.mapReduce(list, mapper, reducer);
out.printf("result: %s%n", result);
out.flush();
}
use of java.util.function.BinaryOperator in project sandbox by backpaper0.
the class ScanCollectorTest method scan.
/**
* <tt>[t<sub>0</sub>, t<sub>1</sub>, t<sub>2</sub> ... t<sub>n</sub>]</tt>というストリームに対して
* <pre>
* r<sub>0</sub> = init
* r<sub>1</sub> = fn(r<sub>0</sub>, t<sub>0</sub>)
* r<sub>2</sub> = fn(r<sub>1</sub>, t<sub>1</sub>)
* r<sub>3</sub> = fn(r<sub>2</sub>, t<sub>2</sub>)
* .
* .
* .
* r<sub>n+1</sub> = fn(r<sub>n</sub>, t<sub>n</sub>)
* </pre>
* と処理して<tt>[r<sub>0</sub>, r<sub>1</sub>, r<sub>2</sub> ... r<sub>n+1</sub>]</tt>というリストを返します。
*
* @param init
* @param fn
* @return
*/
static <T, R> Collector<T, ?, List<R>> scan(final R init, final BiFunction<R, T, R> fn) {
final Supplier<LinkedList<R>> supplier = () -> new LinkedList<>(Collections.singletonList(init));
final BiConsumer<LinkedList<R>, T> accumulator = (acc, t) -> acc.add(fn.apply(acc.getLast(), t));
final BinaryOperator<LinkedList<R>> combiner = (acc1, acc2) -> {
acc1.addAll(acc2);
return acc1;
};
final Function<LinkedList<R>, List<R>> finisher = acc -> acc;
final Characteristics characteristics = Characteristics.IDENTITY_FINISH;
return Collector.of(supplier, accumulator, combiner, finisher, characteristics);
}
use of java.util.function.BinaryOperator in project TuringJavaSE3rdBatch by mrthetkhine.
the class ReduceDemo method main.
public static void main(String[] args) {
Random random = new Random();
ArrayList<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < 10; i++) {
list.add(random.nextInt(10) + 1);
}
for (Integer i : list) {
System.out.println("Item " + i);
}
BinaryOperator<Integer> acc = (a, b) -> {
System.out.println("A " + a + " B " + b);
return a < b ? a : b;
};
Optional<Integer> sum = list.stream().reduce(acc);
System.out.println("Min " + sum.get());
}
Aggregations