函数式接口
函数式接口
函数式接口的特点?
接口有且仅有一个抽象方法,抽象方法只没有实现的方法
允许定义静态非抽象方法
允许定义默认default非抽象方法(default方法也是java8才有的)
允许java.lang.Object中的public方法,如上图的方法equals
@FunctionInterface注解不是必须的
实践
编写自定义的Compatator接口
@Test
public void test7() {
Employee e1 = new Employee(1, 21, "zhangsan", "F");
Employee e2 = new Employee(2, 45, "lisi", "M");
Employee e3 = new Employee(3, 60, "wangwu", "M");
Employee e4 = new Employee(4, 32, "zhouliu", "F");
Employee e5 = new Employee(5, 28, "zhaoqi", "M");
Employee e6 = new Employee(6, 43, "qianba", "F");
List<Employee> list = Arrays.asList(e1, e2, e3, e4, e5, e6);
list.sort(new Comparator<Employee>() {
@Override
public int compare(Employee o1, Employee o2) {
return o2.getAge() - o1.getAge();
}
});
list.forEach(System.out::println);
}