//需求:我们要实现自定义学生类的排序去重,并以键值对的形式储存学生对象和学号,当学生姓名和年龄一样的时候我们认为是一个学生 ,我们该怎么做?
import java.util.ArrayList;
import java.util.Comparator;
import java.util.TreeMap;
public class ArrayListDamo {
public static void main(String[] args) {
//ArrayList<Student> al=new ArrayList<Student>();
TreeMap<Student,String>tm=new TreeMap<Student,String>(new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
// TODO Auto-generated method stub
int num=o2.getName().compareTo(o2.getName());
int num2=num==0? o2.getAge()-o1.getAge():num;
return num2;
}
});
Student s1=new Student("张",17);
Student s2=new Student("黄晓明",19);
Student s3=new Student("张",17);
Student s4=new Student("周润发大哥",43);
tm.put(s1, "001");
tm.put(s2, "002");
tm.put(s3, "003");
tm.put(s4, "001");
System.out.println(tm);
}
}
|