1、创建实体类
public class Person {
private int id;
private String name;
private int age;
public Person(int id,String name,int age){
this.id = id;
this.name = name;
this.age = age;
}
2、去重 排序
public static void main(String[] args) {
List<Person> personList = new ArrayList<>();
personList.add(new Person(1,"0002",5));
personList.add(new Person(2,"0004",7));
personList.add(new Person(3,"0001",8));
personList.add(new Person(4,"0003",6));
personList.add(new Person(5,"0003",6));
System.out.println(personList);
// 去重和按照对象某个属性排序
Set<Person> personSet = new TreeSet<>(new Comparator<Person>(){
@Override
public int compare(Person a,Person b){
return a.getName().compareTo(b.getName());
}
});
if (personList != null && !personList.isEmpty()) {
personList.forEach(person -> {
personSet.add(person);
});
}
System.out.println(personSet);
}