【数组、ArrayList 、TreeMap的排序方法】自定义类如何排序,存入数组排序、存入ArrayList排序、存入TreeMap排序

news/2025/3/14 22:37:01/

目录

  • 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

http://www.ppmy.cn/news/813780.html

相关文章

反直觉!一种新方法或让AI模型拥有“联想”力,甚至能识别从未见过的事物

反直觉&#xff01;一种新方法或让AI模型拥有“联想”力&#xff0c;甚至能识别从未见过的事物 一种新的方法正在让人工智能模型获得人类的 “联想” 能力&#xff0c;甚至能让它识别此前从未见过的事物。 来自加拿大滑铁卢大学的博士生伊利亚&#xff08;Ilia Sucholutsky&…

反直觉!一种新方法或让AI模型拥有“联想”力,甚至能识别从未见过的事物-1

一种新的方法正在让人工智能模型获得人类的 “联想” 能力,甚至能让它识别此前从未见过的事物。 来自加拿大滑铁卢大学的博士生伊利亚(Ilia Sucholutsky)和他的博士导师马赛厄斯・尚劳(Matthias Schonlau)教授,首次提出了 “少于一次” 样本学习的概念和方法,并由此为人…

水库监测中仪器安装及监测结果的要求有哪些

水库监测点位布设需要根据水库运行情况和安全监测的需求来进行&#xff0c;一般分为基础监测点位和重要部位监测点位&#xff0c;基础监测点位主要包括上游水位、上游库水位变幅、库岸稳定以及上下游坝坡稳定等。重要部位监测点位主要包括坝轴线、溢洪道进口和泄水洞出口等部位…

基于STM32设计的酒精检测仪

一、需求分析 随着社会的发展和生活水平的提高&#xff0c;人们对于行车安全、家庭安全的要求越来越高&#xff0c;而酒驾等问题也日渐突出&#xff0c;为此&#xff0c;开发一款基于STM32的酒精检测仪&#xff0c;通过检测酒精浓度&#xff0c;实时显示结果并进行报警&#x…

中国式的父慈子孝:爸妈用子女旧手机

中国式的父慈子孝&#xff1a;爸妈用子女旧手机 许多年轻人更换手机频率高&#xff0c;淘汰的旧手机普遍都留给父母使用。但是子女拿回家的智能手机&#xff0c;有的父母因为使用智能手机不习惯而遭遇尴尬。 今年27岁的市民小李&#xff0c;去年刚花4000多元买了一部手机&#…

计算机主板属于什么垃圾分类类别,【垃圾分类】旧充电宝属于什么垃圾

原标题&#xff1a;【垃圾分类】旧充电宝属于什么垃圾 充电宝是生活中比较常见的一种电子产品&#xff0c;它主要是用来充电用的&#xff0c;使用方便易于携带。那么充电宝属于有害垃圾还是可回收物呢&#xff1f; 旧充电宝属于什么垃圾 旧充电宝属于可回收垃圾。为什么充电电池…

让旧手机再用10年靠谱吗?!一款开源Linux已激活150款设备

导读&#xff1a; 你是不是也有很多旧手机尽管硬件没有什么损坏&#xff0c;但由于厂商不再更新系统了&#xff0c;只能让旧手机躺在抽屉里等着寿终正寝。下面编者给你带来一则好消息。 最近&#xff0c;编者注意到一则关于在iPhone 7上成功引导Linux操作系统的新闻&#xff0c…

tomcat java垃圾回收_垃圾回收器及tomcat调优

引用计数法 概述&#xff1a;给对象中添加一个引用计数器&#xff0c;每当有一个地方引用它时&#xff0c;计数器值就加1&#xff1b;当引用失效时&#xff0c;计数器值就减1&#xff1b;任何时刻计数器都为0的对象就是不再被使用的&#xff0c;垃圾收集器将回收该对象使用的内…