TreeSet

TreeSet实现了SortedSet接口,能够对集合中的对象进行排序 。

它支持两种排序方式:

一、自然排序

      使用自然排序时,向TreeSet集合里加入的对象都要实现了Comparable接口(里面的compareTo()方法用于指定排序方式)。

 1 package test.java;
 2 
 3 import java.util.Iterator;
 4 import java.util.Set;
 5 import java.util.TreeSet;
 6 
 7 /**
 8  * Created by Lenovo on 2015/2/10.
 9  */
10 public class TreeSetTest implements Comparable {
11 
12 
13     public String name;
14     public int age;
15 
16     public TreeSetTest(String name, int age) {    //构造方法没有返回值
17         this.name = name;
18         this.age = age;
19     }
20     public String getName(){
21         return this.name;
22     }
23     public int getAge(){
24         return this.age;
25     }
26     public boolean equals(Object o) {
27         if (this == o) return true;
28         if (!(o instanceof Customer)) {
29             return false;
30         }
31         TreeSetTest other = (TreeSetTest) o;  // 记得!!!
32         return (this.name.equals(other.getName()) && this.getAge() == other.age);
33 
34     }
35 
36     public int hashCode() {
37         int result;
38         result = (name == null) ? 0 : name.hashCode();
39         result = 29 * result + age;
40         return result;
41     }
42 
43     @Override
44     public int compareTo(Object o) {
45 
46         TreeSetTest other = (TreeSetTest) o;
47         if (this.name.compareTo(other.name) > 0)
48             return 1;
49         if (this.name.compareTo(other.name) < 0)
50             return -1;
51         if(this.age>other.age)
52             return 1;
53         if(this.age<other.age)
54             return -1;
55         return 0;
56     }
57 
58     public static void main(String[] args) {
59         Set<TreeSetTest> set=new TreeSet<TreeSetTest>();
60         TreeSetTest t1=new TreeSetTest("Tom",15);
61         TreeSetTest t2=new TreeSetTest("Tom",16);
62         TreeSetTest t3=new TreeSetTest("Alice",33);
63 
64         set.add(t1);
65         set.add(t2);
66         set.add(t3);
67         Iterator<TreeSetTest> it=set.iterator();
68         while(it.hasNext()) {
69             TreeSetTest temp = it.next();
70             System.out.println(temp.name + "  " + temp.age);
71         }
72     }
73 }

        为了保证TreeSetTest能够正确的排序,要求它的compareTo方法和equals方法按相同的规则比较两个对象是否一样,当一个类实现了equals 方法,它的hashCode方法也要实现且保证当两个对象相等时它的哈希码是一样的 。

      对于TreeSet中已经存在的对象,若修改了他们的属性,TreeSet不会对集合重新排序。因此适合用TreeSet进行排序的是不可变类。

 

 

二、客户排序

      客户化排序实现java.util.Comparator<Type> 接口,<Type>指定被排序的对象的类型,里面的compare 方法指定排序的规则。

Customer类:

 1 package test.java;
 2 
 3 import java.util.Objects;
 4 
 5 public class Customer implements Cloneable{
 6 
 7     private String name;
 8     private  int age;
 9 
10     public Customer(){
11         this("unknow",0);  //显示的默认构造方法中 第一个语句必须是this语句。
12 
13     }
14 
15     public Customer(String name,int age){    //构造方法没有返回值
16         this.name=name;
17         this.age=age;
18     }
19 
20     public String getName(){
21         return this.name;
22     }
23 
24     public int getAge(){
25         return this.age;
26     }
27 
28     public Object clone()throws CloneNotSupportedException{
29         return super.clone();
30     }
31 
32     public boolean equals(Object o){
33         if(this==o) return true;
34         if(!(o instanceof Customer)){
35             return false;
36         }
37         Customer other= (Customer ) o;  // 记得!!!
38         return (this.name.equals( other.name)&&this.age==other.age);
39 
40     }
41 
42     public String toString(){return this.name+"  "+this.age ;}
43 
44 
45 }
View Code

 

 1 package test.java;
 2 
 3 import java.util.Comparator;
 4 import java.util.Iterator;
 5 import java.util.Set;
 6 import java.util.TreeSet;
 7 
 8 /**
 9  * Created by Lenovo on 2015/2/10.
10  */
11 public class CustomerComparator implements Comparator<Customer> {
12 
13 
14     @Override
15     public int compare(Customer o1, Customer o2) {
16         if (o1.getName().compareTo(o2.getName()) > 0) return -1;
17         if (o1.getName().compareTo(o2.getName()) < 0) return 1;
18 
19         if(o1.getAge()<o2.getAge())
20             return -1;
21         if(o1.getAge()>o2.getAge())
22             return 1;
23         return 0;
24     }
25 
26     public static void main(String[] args) {
27         Set<Customer> set = new TreeSet<Customer>(new CustomerComparator());
28         Customer t1 = new Customer("Tom", 15);
29         Customer t2 = new Customer("Tom", 16);
30         Customer t3 = new Customer("Alice", 33);
31 
32         set.add(t1);
33         set.add(t2);
34         set.add(t3);
35 
36         Iterator<Customer> it = set.iterator();
37         while (it.hasNext()) {
38             Customer temp = it.next();
39             System.out.println(temp.getName() + " & " + temp.getAge());
40         }
41     }
42 }

 

posted @ 2015-02-10 14:47  galaxy77  阅读(167)  评论(0编辑  收藏  举报