集合框架之Map TreeMap

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 }

 

posted @ 2019-05-06 19:26  luojack  阅读(239)  评论(0编辑  收藏  举报