ファンクショナルインターフェース 2019/7

Consumer 引数あり・戻り値なし
acceptで呼び出す。
プリミティブ型用にIntConsumer, LongConsumer, DoubleConsumerが用意されている。
引数二つの場合はBiConsumerを使用する。
Consumer cs = arg -> System.out.println(arg);
cs.accept("Hello!");
cs.accept(1);
cs.accept(BigDecimal.ZERO);
Suppiler 引数なし・戻り値あり
getで呼び出す。
プリミティブ型用にBooleanSupplier, IntSupplier, LongSupplier, DoubleSupplierが用意されている。
Supplier sp = () -> LocalDateTime.now().plusHours(-8);
System.out.println(sp.get());  //2019-07-06T07:03:53.413
Thread.sleep(10000);
System.out.println(sp.get());  //2019-07-06T07:04:03.420
Predicate<T> 引数あり・戻り値はboolean
testで呼び出す。
Predicateインスタンス同士をand, orで連結評価できる。
Predicate<Double> pd = arg -> Math.abs(arg - 3.14) < 0.1;
System.out.println(pd.test(3.25)); // false
System.out.println(pd.test(3.16)); // trues
UnaryOperator<T> 引数と戻り値が同じ型。
プリミティブ型用にIntUnaryOperator, LongUnaryOperator, DoubleUnaryOperatorが用意されている。
UnaryOperator<BigInteger> uo = arg -> arg.add(arg).nextProbablePrime();
System.out.println(uo.apply(new BigInteger("100")));
System.out.println(uo.apply(new BigInteger("300")));
BinaryOperator<T> 2つの引数と戻り値が同じ型。
* プリミティブ型用にIntBinaryOperator, LongBinaryOperator, DoubleBinaryOperatorが用意されている。
BinaryOperator<BigInteger> bo = (arg1, arg2) -> arg1.abs().add(arg2.abs());
System.out.println(bo.apply(new BigInteger("-100"), new BigInteger("-100")));
Function<T, R> 引数一つ・戻り値あり。引数、戻り値ともに型は任意。
プリミティブ型用にIntFunction<R>, LongFunction<R>, DoubleFunction<R>が用意されている。
Function<BigDecimal, String> fn = arg -> arg.add(arg).toString();
System.out.println(fn.apply(new BigDecimal("99")));