「Java8」インタフェースFunctionを使うサンプル
説明
型パラメータ:
T – 関数の入力の型
R – 関数の結果の型
使用例
package com.example.demo;
import java.util.function.Function;
public class PredicateDemo {
public static void main(String[] args) {
test1();
test2();
}
// Function<Integer,String>
public static void test1() {
Function<Integer, String> y = x -> x * 2 + " test ";
String result = y.apply(5);
System.out.println("result:" + result);
}
// Function<Integer,Integer>
public static void test2() {
Function<Integer, Integer> y = x -> x * 2 + 10;
int result = y.apply(5);
System.out.println("result:" + result);
}
}
実行結果
result:10 test
result:20