1 局部类
public static void main(String[] args) {
List humans = Human.getAInitHumanList();
//方法内-局部类
class HumanComparetor implements Comparator {
@Override
public int compare(Human h1, Human h2) {
return h1.getAge() - h2.getAge();
}
}
Collections.sort(humans, new HumanComparetor());
System.out.println(humans);
}
2 局部类改为匿名类
public static void main(String[] args) {
List humans = Human.getAInitHumanList();
//匿名类
Collections.sort(humans, new Comparator() {
@Override
public int compare(Human h1, Human h2) {
return h1.getAge() - h2.getAge();
}
});
System.out.println(humans);
}
3
2的lamdba表达式
public static void main(String[] args) {
List humans = Human.getAInitHumanList();
//lamdba 表达式 ->
Collections.sort(humans, (Human h1, Human h2) -> h1.getAge() - h2.getAge());
System.out.println(humans);
}