代码例子:
/**
* 测试Comparator
*/
@Test
public void test17() {
//原始方法
Comparator<Integer> comparator = new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return Integer.compare(o1, o2);
}
};
int compare1 = comparator.compare(10, 11);
System.out.println(compare1);
System.out.println("********");
//使用lambda
Comparator<Integer> c2 = (o1, o2) -> Integer.compare(o1, o2);
int compare2 = c2.compare(11, 10);
System.out.println(compare2);
System.out.println("********");
//方法引用
Comparator<Integer> c3 = Integer::compareTo;
int compare3 = c3.compare(10, 11);
System.out.println(compare3);
}