接口中只有一个抽象方法的口,称为函数式接口,可以使用注解@FunctionInterface 修饰
@FunctionInterface : 可以检查接口是否为函数式接口
void accept(T t);
@Test
public void test5(){
happy("hello world", str -> System.out.println("this is " + str));
}
public void happy(String str, Consumer consumer){
consumer.accept(str);
}
T get();
@Test
public void test6(){
List num = getNum(10, () -> (int) (Math.random() * 100));
num.forEach(System.out::println);
}
public List getNum(int num, Supplier supplier){
ArrayList integers = new ArrayList<>();
for (int i = 0; i < num; i++) {
integers.add(supplier.get());
}
return integers;
}
R apply(T t);
@Test
public void test7(){
String hello_world = stringHandler("hello world", s -> s.substring(0, 5));
System.out.println(hello_world);
}
public String stringHandler(String string, Function<String,String> function){
return function.apply(string);
}
boolean test(T t);
@Test
public void test8(){
boolean query = isQuery("select query", s -> s.equals("select query"));
System.out.println(query);
}
public boolean isQuery(String string, Predicate predicate){
return predicate.test(string);
}