java学习之treeset

Set:无序,不可以重复元素

  |--HashSet:数据结构是哈希表,线程是非同步的。

          保证元素唯一性的原理:判断元素的hashcode值是否相同

            如果相同,还会继续判断元素的equals方法,是否为true

  |--TreeSet:可以对Set集合中的元素进行排序。

        底层数据结构是二叉树。

        保证元素唯一性的依据:compareTo方法return 0

    

            TreeSet排序的第一种方式:让元素自身具备比较性。

                    元素需要实现Compareable接口,覆盖compareTo方法。

                    这种方式也称为元素的自然顺序,或者叫做默认顺序。

        第二种方式:当元素自身不具备比较性时,或具备的比较性不是所需要的,

              就需要集合自身具备比较性。定义比较器,将比较器对象作为参数

              传递给TreeSet集合的构造函数。

        当两种方式都存在时以比较器为主。

注意:排序时,当主要条件相同时,一定要判断一下次要条件(否则会被认为是同一个对象而不被加入)

 1 import java.util.Iterator;
 2 import java.util.TreeSet;
 3 
 4 /**
 5  * 需求:
 6  * 往treeset集合中存储自定义对象学生  
 7  * 想按照学生的年龄进行排序
 8  */
 9 public class PackageDemo {
10 
11     public static void main(String[] args) {
12         TreeSet ts=new TreeSet();
13         ts.add(new Student("皮卡丘1",20));
14         ts.add(new Student("皮卡丘",20));
15         ts.add(new Student("皮卡",19));
16         ts.add(new Student("雷卡",21));
17         ts.add(new Student("雷卡",21));
18         
19         Iterator it=ts.iterator();
20         
21         while(it.hasNext()) {
22             Student student=(Student)it.next();
23             System.out.println(student.getName()+"...."+student.getAge());
24         }
25         
26     }
27 
28 }
29 class Student implements Comparable{//该接口强制让学生具有比较性
30     private String name;
31     private int age;
32     public Student(String name,int age) {
33         this.name=name;
34         this.age=age;
35     }
36     public String getName() {
37         return name;
38     }
39     public int getAge() {
40         return age;
41     }
42     @Override
43     public int compareTo(Object o) {
44         if(null == o) {
45             throw new RuntimeException("传进一个空对象");
46         }
47         if(!(o instanceof Student)) {
48             throw new RuntimeException("传进来的不是学生类");
49         }
50         Student student=(Student)o;
51         if(this.age>student.age) {
52             return 1; 
53         }else if(this.age==student.age) {
54             return this.name.compareTo(student.name);//注意:排序时,当主要条件相同时,一定要判断一下次要条件(否则会被认为是同一个对象而不被加入)
55         }
56         return -1;
57     }
58     
59     
60 }
 1 import java.util.Iterator;
 2 import java.util.TreeSet;
 3 
 4 /**
 5  * 需求:
 6  * 往treeset集合中存储自定义对象学生  
 7  * 想按照学生的年龄进行排序
 8  * 
 9  * 需求变更:
10  * 要求获得的顺序和添加的一致
11  * 方法:利用二叉树特性和从小到大取值的特性,仅需使返回值恒为1:public int compareTo(Object o) {
12         return 1;
13     }
14     
15     同理可得:是返回值恒为0,则不会添加任何的数据,因为被认为是相同的对象而不会加入二叉树
16  * 
17  */
18 public class PackageDemo {
19 
20     public static void main(String[] args) {
21         TreeSet ts=new TreeSet();
22         ts.add(new Student("皮卡丘1",20));
23         ts.add(new Student("皮卡丘",20));
24         ts.add(new Student("皮卡",19));
25         ts.add(new Student("雷卡",21));
26         ts.add(new Student("雷卡",21));
27         
28         Iterator it=ts.iterator();
29         
30         while(it.hasNext()) {
31             Student student=(Student)it.next();
32             System.out.println(student.getName()+"...."+student.getAge());
33         }
34         
35     }
36 
37 }
38 class Student implements Comparable{//该接口强制让学生具有比较性
39     private String name;
40     private int age;
41     public Student(String name,int age) {
42         this.name=name;
43         this.age=age;
44     }
45     public String getName() {
46         return name;
47     }
48     public int getAge() {
49         return age;
50     }
51     @Override
52     public int compareTo(Object o) {
53         return 0;
54 //        return 1;
55         /*if(null == o) {
56             throw new RuntimeException("传进一个空对象");
57         }
58         if(!(o instanceof Student)) {
59             throw new RuntimeException("传进来的不是学生类");
60         }
61         Student student=(Student)o;
62         if(this.age>student.age) {
63             return 1; 
64         }else if(this.age==student.age) {
65             return this.name.compareTo(student.name);
66         }
67         return -1;*/
68     }
69     
70     
71 }
 1 public class TreeSetDemo2 {
 2     public static void main(String[] args) {
 3          TreeSet ts=new TreeSet(new MyCompare());//也可用匿名内部类实现
 4          ts.add(new Student("皮卡丘1",20));
 5          ts.add(new Student("皮卡丘",20));
 6          ts.add(new Student("皮卡",19));
 7          ts.add(new Student("雷卡",21));
 8          ts.add(new Student("雷卡2",21));
 9          
10          Iterator it=ts.iterator();
11          while(it.hasNext()) {
12                 Student student=(Student)it.next();
13                 System.out.println(student.getName()+"...."+student.getAge());
14          }
15     }
16 }
17 class Student implements Comparable{
18     private String name;
19     private int age;
20     public Student(String name,int age) {
21         this.name=name;
22         this.age=age;
23     }
24     public String getName() {
25         return name;
26     }
27     public int getAge() {
28          return age;
29     }
30     public int compareTo(Object o) {
31         return 1;
32     }
33 }
34 class MyCompare implements Comparator{
35 
36     @Override
37     public int compare(Object o1, Object o2) {
38         Student s1=(Student)o1;
39         Student s2=(Student)o2;
40         int num=s1.getName().compareTo(s2.getName());
41         if(num==0) {
42             return new Integer(s1.getAge()).compareTo(s2.getAge());
43         }
44         return num;
45     }
46     
47 }
原文地址:https://www.cnblogs.com/zhaohuan1996/p/8245076.html