目录
- 1 Integer类,自定义Student类定制比较器comparator排序规则
- 2 数组[]排序Array.sort
- 3 ArrayList排序,两种调用排序的方法 1、list.sort(比较器); 2、Collections.sort(list, 比较器);
- 4 TreeMap排序,三种new时有排序的方法1、new TreeMap参数用lambda表达式代替比较器2、TreeMap参数new含比较器的类对象3、 TreeMap参数new含比较器的匿名类对象(new Comparator<Student> (){实现compare方法});
1 Integer类,自定义Student类定制比较器comparator排序规则
//学生类:三个属性public static class Student {String name;int id;int age;public Student(String name, int id, int age) {this.name = name;this.id = id;this.age = age;}}//排序Integer类//降序public static class MyComp implements Comparator<Integer> {@Overridepublic int compare(Integer o1, Integer o2) {return o2 - o1;}}//打印数组public static void printArray(Integer[] arr){for(Integer i : arr){System.out.print(i+",");}System.out.println();}//排序Student类//根据id从小到大,若id同,按年龄从大到小public static class IdShengAgeJiangOrder implements Comparator<Student> {@Overridepublic int compare(Student o1, Student o2) {return o1.id != o2.id ? (o1.id - o2.id) : (o2.age - o1.age);}}//排序Student类//id升序public static class IdAscending implements Comparator<Student> {@Overridepublic int compare(Student o1, Student o2) {return o1.id - o2.id;}}//排序Student类//id降序public static class IdDescending implements Comparator<Student> {@Overridepublic int compare(Student o1, Student o2) {return o2.id - o1.id;}}//打印学生三个属性public static void printStudents(Student[] students){for(Student s : students){System.out.println("Name:"+s.name+" Id:"+s.id+" Age:"+s.age);}}
2 数组[]排序Array.sort
public static void main(String[] args) {//Integer数组用MyComp排序并打印Integer[] arr = { 1, 2, 3, 5, 2, 1 };Arrays.sort(arr, new MyComp());printArray(arr);System.out.println("第一条打印");Student[] stus = {stu1, stu2, stu3, stu4, stu5};Arrays.sort(stus, new IdShengAgeJiangOrder());printStudents(stus);
}
结果:
5,3,2,2,1,1,
第一条打印
Name:E Id:3 Age:25
Name:D Id:3 Age:4
Name:B Id:4 Age:20
Name:A Id:4 Age:18
Name:C Id:4 Age:8
3 ArrayList排序,两种调用排序的方法 1、list.sort(比较器); 2、Collections.sort(list, 比较器);
public static void main(String[] args) {Student stu1 = new Student("A",4,18);Student stu2 = new Student("B",4,20);Student stu3 = new Student("C",4,8);Student stu4 = new Student("D",3, 4);Student stu5 = new Student("E",3, 25);//---------------------------------------------------------------System.out.println("第二条打印");ArrayList<Student> stus2 = new ArrayList<>();stus2.add(stu1);stus2.add(stu2);stus2.add(stu3);stus2.add(stu4);stus2.add(stu5);//5个Student的ArrayList用.sort(比较器)排序/**ArrayList两种调用排序写法-------------------------------------------------*///ArrayList,两种排序都可以,本身是按照添加顺序存储stus2.sort(new IdShengAgeJiangOrder());//♥ Collections.sort(stus2, new IdShengAgeJiangOrder());for(int i = 0; i < stus2.size(); i++){Student s = stus2.get(i);System.out.println(s.name+","+s.id+","+s.age);}}
}
运行结果:
第二条打印
E,3,25
D,3,4
B,4,20
A,4,18
C,4,8
4 TreeMap排序,三种new时有排序的方法1、new TreeMap参数用lambda表达式代替比较器2、TreeMap参数new含比较器的类对象3、 TreeMap参数new含比较器的匿名类对象(new Comparator (){实现compare方法});
System.out.println("第三条打印");stu1 = new Student("A", 4, 40);stu2 = new Student("B", 4, 18);stu3 = new Student("C", 4, 34);stu4 = new Student("D", 4, 89);stu5 = new Student("E", 4, 5);/**TreeMap的3种排序写法---------------------------------------------------------*///5个Student的TreeMap用new时,new时用lambda表达式作为比较器((a,b) -> (a.id - b.id)//缺点,若id相同则只能存储一个,不能再根据age排序//♥TreeMap<Student, String> stus3 = new TreeMap<>((a,b) -> (a.id - b.id));//5个Student的数组,用IdSheng……排序,打印//♥TreeMap<Student, String> stus3 = new TreeMap<>(new IdShengAgeJiangOrder());//直接在参数部分实现匿名类重写比较器TreeMap<Student, String> stus3 = new TreeMap<>(new Comparator<Student> (){@Overridepublic int compare(Student o1, Student o2) {return o1.id != o1.id ? o1.id - o2.id : o1.age - o2.age;}});stus3.put(stu1, "我是学生1,我的名字叫A");stus3.put(stu2, "我是学生2,我的名字叫B");stus3.put(stu3, "我是学生3,我的名字叫C");stus3.put(stu4, "我是学生4,我的名字叫D");stus3.put(stu5, "我是学生5,我的名字叫E");for(Student s : stus3.keySet()){System.out.println(s.name+","+s.id+","+s.age);}
运行结果:
第三条打印
E,4,5
B,4,18
C,4,34
A,4,40
D,4,89