TreeMap的使用

TreeMap<K,V>(树映射)不同于TreeSet,TreeSet(树集)用于对数据进行排序,而TreeMap的节点存储"关键字/值"对,保证节点按照关键字升序排列

 1 package test;
 2 
 3 import java.util.Collection;
 4 import java.util.Iterator;
 5 import java.util.TreeMap;
 6 
 7 public class test1 {
 8 
 9     public static void main(String[] args) {
10         
11         Student st[] = new Student[4];
12         String str[] = {"小明","小王","小李","小张"};
13         int math[] = {50,60,80,81};
14         int english[] = {90,60,70,80};
15         
16         TreeMap<studentKey,Student> treemap = new TreeMap<studentKey,Student>();
17         
18         for(int i=0;i<str.length;i++){
19             st[i] = new Student(math[i],english[i],str[i]);
20         }
21         
22         studentKey key[] = new studentKey[4];
23         for(int i=0;i<key.length;i++){
24             //按照数学成绩排序
25             key[i] = new studentKey(st[i].math);
26         }
27         for(int i=0;i<key.length;i++){
28             treemap.put(key[i], st[i]);
29         }
30         
31         Collection<Student> col = treemap.values();
32         Iterator<Student> it = col.iterator();
33         
34         System.out.println("按照数学成绩排序");
35         while(it.hasNext()){
36             Student stu = it.next();
37             System.out.println("姓名:"+stu.name+"  "+"成绩:"+stu.math);
38         }
39         
40         //clear tree
41         treemap.clear();
42         
43         for(int i=0;i<key.length;i++){
44             //按照英语成绩排序
45             key[i] = new studentKey(st[i].English);
46         }
47         for(int i=0;i<key.length;i++){
48             treemap.put(key[i], st[i]);
49         }
50         
51         Collection<Student> col01 = treemap.values();
52         Iterator<Student> it01 = col.iterator();
53         
54         System.out.println("\n按照英语成绩排序");
55         while(it01.hasNext()){
56             Student stu = it01.next();
57             System.out.println("姓名:"+stu.name+"  "+"成绩:"+stu.English);
58         }
59                 
60     }
61 }
62 
63 class Student{
64     
65     public int math,English;
66     public String name;
67 
68     public Student(int math, int english, String name) {
69         super();
70         this.name = name;
71         this.math = math;
72         English = english;
73     }
74 }
75 
76 class studentKey implements Comparable<Object>{
77 
78     private int score;
79     
80     public studentKey(int score) {
81         super();
82         this.score = score;
83     }
84     @Override
85     public int compareTo(Object o) {
86         // TODO Auto-generated method stub
87         studentKey sk = (studentKey)o;
88         if((this.score - sk.score) == 0){
89             return 1;
90         }else {
91             return (this.score - sk.score);
92         }
93     }
94 }

 其中关键字类要实现Comparable接口,重写compareTo方法,其中需要注意一点,树集,树映射中不允许出现大小相等的两个节点,如果出现相同节点,第二个节点视为无效,如果允许成绩相同,需将compareTo方法按照如上方式重写。

运行结果:

按照数学成绩排序
姓名:小明  成绩:50
姓名:小王  成绩:60
姓名:小李  成绩:80
姓名:小张  成绩:81

按照英语成绩排序
姓名:小王  成绩:60
姓名:小李  成绩:70
姓名:小张  成绩:80
姓名:小明  成绩:90

 

posted on 2015-09-09 09:28  TGomving  阅读(248)  评论(0编辑  收藏  举报