当前位置: 首页 > news >正文

TreeMap类型实体类数据进行排序

实体类Student类代码如下所示:

package com.test.Test11;public class Student implements Comparable<Student>{private int age;private String name;private Double height;public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Double getHeight() {return height;}public void setHeight(Double height) {this.height = height;}public Student(int age, String name, Double height) {this.age = age;this.name = name;this.height = height;}@Overridepublic String toString() {return "Student{" +"age=" + age +", name='" + name + '\'' +", height=" + height +'}';}@Overridepublic int compareTo(Student o) {/*//按照年龄排序return this.getAge()-o.getAge();*///按照姓名排序return this.getName().compareTo(o.getName());}
}测试类Test.java如下所示:
package com.test.Test11;import java.util.TreeMap;public class Test03 {public static void main(String[] args) {//TreeMap 唯一,有序(按升序或者降序排序)TreeMap<Student, Integer> map = new TreeMap<>();map.put(new Student(19,"blili",170.5), 1001);map.put(new Student(18,"blili",150.5), 1003);map.put(new Student(19,"alili",180.5), 1023);map.put(new Student(17,"clili",140.5), 1671);map.put(new Student(10,"dlili",160.5), 1891);System.out.println(map.size()); //4(上面的添加了5对数据,只出来4对数据)//按照age升序排列System.out.println(map);//按照年龄排序{Student{age=10, name='dlili', height=160.5}=1891, Student{age=17, name='clili', height=140.5}=1671, Student{age=18, name='blili', height=150.5}=1003, Student{age=19, name='blili', height=170.5}=1023}//按照姓名排序 {Student{age=19, name='alili', height=180.5}=1023, Student{age=19, name='blili', height=170.5}=1003, Student{age=17, name='clili', height=140.5}=1671, Student{age=10, name='dlili', height=160.5}=1891}}
}
http://www.lryc.cn/news/92373.html

相关文章:

  • HOOPS助力AVEVA数字化转型:支持多种3D模型格式转换!
  • (转载)基于遗传模拟退火的聚类算法(matlab实现)
  • 【C++】struct 和 class 的区别
  • 活动笔记丨物业行业人效提升与灵活用工新路径
  • 学习笔记:吴恩达ChatGPT提示工程
  • POI in Action
  • 苹果Vision Pro将引爆人机交互的重大变革
  • MMDetection学习记录(二)之配置文件
  • Python数据分析:NumPy、Pandas和Matplotlib的使用和实践
  • 实习生面试问题及回答记录
  • 设计模式(十):结构型之外观模式
  • 买法拍房需要注意什么
  • linux命令输出结果但不显示在屏幕上的通用办法
  • 【Linux系统进阶详解】Linux字符权限rwx-权限组合原理,对应类型ugo,user,group,other,+-=详解及权限管理实战
  • 凡人修C传——专栏从凡人到成仙系列目录
  • 隐藏python代码,售卖并保护源代码
  • Material—— VAT(Houdini To UE)
  • 视频后期剪辑
  • Python3+Selenium2完整的自动化测试实现之旅(七):完整的轻量级自动化框架实现
  • 泰山信息科技5周年:无尽的感恩,非常非常的惋惜
  • LabVIEW编程开发PCB测试仪
  • React使用Electron开发桌面端
  • springboot+vue餐厅点餐系统在线点餐系统(含源码+数据库)
  • Vue.js 中的 TypeScript 支持是什么?如何使用 TypeScript?
  • 测试者必知—如何做Web测试?常见测试点总结
  • 怎么转换英文音频成文字?英文音频转文字app分享
  • esp32-cam拍照上传,app inventor 制作安卓app实时显示
  • 基于jsp+mysql+Spring+mybatis+Springboot的Springboot实现的就业信息管理平台
  • 阿里巴巴内部10w字Java面试小抄火了,完整版开放下载
  • Logback自定义DBAppender保存系统日志到数据库