groovy789's blog

技術系備忘録φ(..)メモメモ

java8で追加されたjava.util.functionパッケージ。
事前定義されているclosureを実行してみる。

public class Smaple {
    public static void main(String[] args) {
        // 1つの引数と1つの戻り値
        Function<String, String> function = s -> "hello world";
        System.out.println(function.apply(""));
        // 1つの引数とbooleanの戻り値
        Predicate<String> predicate = s -> true;
        System.out.println(predicate.test(""));
        // 1つの引数と戻り値なし
        Consumer<String> consumer = s -> System.out.println("hello world");
        consumer.accept("");
        // 引数なしと1つの戻り値
        Supplier<String> supplier = () -> "hello world";
        System.out.println(supplier.get());
        // 2つの引数と1つの戻り値
        BinaryOperator<String> binaryOperator = (s1, s2) -> "hello world";
        System.out.println(binaryOperator.apply("", ""));
        // 独自定義
        MyFunction myFunction = (a,b,c) -> System.out.println("hello world");
        myFunction.apply("", 0, "");
    }
    
    @FunctionalInterface
    interface MyFunction {
        void apply(String a, Integer b, Object c);
    }
}