Map(3)--treeMap的排序
来源:互联网 发布:数据中标网 编辑:程序博客网 时间:2024/06/06 00:30
当key是Student类对象,value是String型时,
(一) 在Student类中重写compareTo方法
@Overridepublic int compareTo(student o) { int num=this.age-o.age; return num==0?this.name.compareTo(o.name):num;}
回到主函数,
public static void demo1() { TreeMap<student,String> tm=new TreeMap<student,String>(); tm.put(new student("张三",23), "北京"); tm.put(new student("赵六",13), "上海"); tm.put(new student("李四",24), "广州"); tm.put(new student("王五",25), "深圳"); System.out.println(tm);}
(二)传入比较器Comparator《E》
public static void demo2() { TreeMap<student,String> tm=new TreeMap<student,String>( new Comparator<student>(){ @Override public int compare(student s1, student s2) { int num=s1.getName().compareTo(s2.getName()); return num==0?s1.getAge()-s2.getAge():num; } }); tm.put(new student("张三",23), "北京"); tm.put(new student("赵六",13), "上海"); tm.put(new student("李四",24), "广州"); tm.put(new student("王五",25), "深圳"); System.out.println(tm);}
阅读全文
0 0
- Map(3)--treeMap的排序
- TreeMap排序和Map的遍历
- Map 排序 TreeMap 陷阱
- Map ,HashMap , TreeMap , TreeMap 默认排序
- Map ,HashMap , TreeMap , TreeMap 默认排序
- Java中对Map(HashMap,TreeMap,Hashtable等)的排序
- 关于map的数据排序问题(hashmap,treemap)
- Java中对Map(HashMap,TreeMap,Hashtable等)的排序
- 能够排序的Map实现类TreeMap类
- 【Java基础】Map集合简介 排序的集合TreeMap
- 能够排序的Map实现类TreeMap类
- Java TreeMap的排序
- Java TreeMap的排序
- Java TreeMap的排序
- Java map排序(TreeMap,LinkedHashMap)
- 利用TreeMap对map进行排序
- Map中的TreeMap根据value排序
- Map的hashMap和TreeMap
- Git使用手册:Git的使用:工作区、暂存区、本地仓库、远程仓库
- 浅析指针
- Macaca 实现 iosMonkey (python 版)
- 搭建环境,安装python+selenium
- android对数据按照时间排序
- Map(3)--treeMap的排序
- linux的uuid
- JS中值类型与引用类型分析
- (2)操作系统运行环境与运行机制
- windows 问题 LoadLibrary失败,GetLastError= 193
- Sodility配置本地IDE和共享目录
- JAVA实现UTC时间转换成北京时间
- Json转Map,List
- 如何在本机编译android8.0环境,适用于Ubuntu16.04