Lambda表达式示例
1.接口
public interface LambdaInterface {
List<String> searchNames();
}
2.需要调用接口方法的类(类似访问者模式)
public class School {
public List<String> selectStudentNames(LambdaInterface lambdaInterface){
System.out.println("准备调用接口方法:");
List<String> list = lambdaInterface.searchNames();
System.out.println("接口方法已调用:" + list.getClass() + list);
return list;
}
}
3.类方法的调用
3.1.匿名内部类调用
public static void main(String[] args) {
School school = new School();
//匿名内部类 new LambdaInterface()
school.selectStudentNames(new LambdaInterface() {
@Override
public List<String> searchNames() {
return new LinkedList<>();
}
});
}
3.2.Lambda表达式调用
public static void main(String[] args) {
School school = new School();
//lambda表达式
school.selectStudentNames(() -> new ArrayList<>());
}