首页 > 其他 > 详细

集合框架之Map TreeMap

时间:2019-05-06 19:38:29      阅读:114      评论:0      收藏:0      [点我收藏+]

TreeMap是Map接口的实现类,key以TreeSet方式存储。

注意:TreeMap为二叉树存储,所以要在key中实现比较策略。

 1 public class Demo2 {
 2 
 3     public static void main(String[] args) {
 4         /*TreeMap<Student,Object> treeMap=new  TreeMap<Student,Object>();*/
 5         TreeMap<Student,Object> treeMap=new  TreeMap<Student,Object>(new Comparator<Student>() {
 6 
 7             @Override
 8             public int compare(Student o1, Student o2) {
 9                 
10                 return o1.getAge()-o2.getAge();
11             }
12             
13         });
14         
15         Student student=new Student(1, "xiaoming", 20);
16         ArrayList<String> list=new ArrayList<String>();
17         list.add("111");
18         list.add("2333");
19         list.add("789");
20         
21         Student student2=new Student(1, "xiaoming", 20);
22         
23         
24         
25         treeMap.put(student, list);
26         treeMap.put(student2, list);
27         System.out.println(treeMap);
28 
29     }
30 
31 }

 

集合框架之Map TreeMap

原文:https://www.cnblogs.com/luojack/p/10821522.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!