#jdk在java.util.funtion中定义了很多函数式接口,方便我们在使用lambda定义接口;
常用接口函数:
1.Supplier提供者:提供数据
@FunctionalInterface
public interface Supplier<T> {
/**
* Gets a result.
*
* @return a result
*/
T get();
}
//声明
public static void text2(Supplier<Integer> supplier){//使用function包下的函数接口supplier(生产数据)
//使用lambda表达式不关心接口名称,方法名称。
//lambda表达式只关心方法的形参,方法的返回值
Integer integer = supplier.get();
}
//使用
Text.text2(()-> 9+10);
//结果
integer = 19;
#区别
public class Main {
public static void main(String[] args) {
//使用lambda表达式不关心接口名称,方法名称。
//lambda表达式只关心方法的形参,方法的返回值
Text.text(()-> System.out.println("使用lambda表达式"));
Text.text2(()-> 9+10);
}
}
2、Consumer<T> 消费者:消费数据
@FunctionalInterface
public interface Consumer<T> {
/**
* Performs this operation on the given argument.
*
* @param t the input argument
*/
void accept(T t);
}
......未完待续,大致是用法是这样的