package com.lhy.sec.test;
public class Person {
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
package com.lhy.sec.test;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
public class ComparatorTest {
public static void main(String[] args) {
Person p1= new Person();
p1.setName("zhangsan");
p1.setAge(24);
Person p2= new Person();
p2.setName("lisi");
p2.setAge(16);
Person p3= new Person();
p3.setName("wangwu");
p3.setAge(31);
List<Person> list = new ArrayList<Person>();
list.add(p1);
list.add(p2);
list.add(p3);
System.out.println("正序:");
list.stream().sorted(Comparator.comparingInt(Person::getAge)).forEach(p-> System.out.println("name:"+p.getName()+", age:"+p.getAge()));
// list.stream().forEach(p-> System.out.println("name:"+p.getName()+", age:"+p.getAge()));
System.out.println("倒序:");
list.stream().sorted(Comparator.comparingInt(Person::getAge).reversed()).forEach(p-> System.out.println("name:"+p.getName()+", age:"+p.getAge()));
// list.stream().forEach(p-> System.out.println("name:"+p.getName()+", age:"+p.getAge()));
System.out.println("run end");
}
}