v: ititit111222333
app
public class FunctionInterface {dom
public static void main(String[] args) {ide
//////////////////////////////////////////////////////////////////////////////函數
consumerFunction("1", (consumer) -> System.out.println("消費了" + consumer + "元"));get
//////////////////////////////////////////////////////////////////////////////it
List<Integer> integers = supplierFunction(10, () -> (int) (Math.random() * 100));io
integers.forEach(System.out::println);入門
//////////////////////////////////////////////////////////////////////////////function
String s = functionFunction("\t\t\t, 我一一iii ", String::trim);class
System.out.println(s);
//////////////////////////////////////////////////////////////////////////////
boolean b = predicateFunction("111111111", "111111111"::equals);
System.out.println(b);
}
/**
* 消費型
*/
private static void consumerFunction(String str, Consumer<String> consumer) {
consumer.accept(str);
}
/**
* 供給型
*/
private static List<Integer> supplierFunction(int num, Supplier<Integer> supplier) {
List<Integer> list = Lists.newArrayList();
for (int i = 0; i < num; i++) {
list.add(supplier.get());
}
return list;
}
/**
* 函數型
*/
private static String functionFunction(String str, Function<String, String> function) {
return function.apply(str);
}
/**
* 函數型
*/
private static boolean predicateFunction(String str, Predicate<String> function) {
return function.test(str);
}
}