java中的set接口有如下的特點:
不允許出現重復元素;
集合中的元素位置無順序;
有且只有一個值為null的元素。
因為java中的set接口模仿了數學上的set抽象,所以,對應的數學上set的特性為:
互異性:一個集合中,任何兩個元素都認為是不相同的,即每個元素只能出現一次。
無序性:一個集合中,每個元素的地位都是相同的,元素之間是無序的。集合上可以定義序關系,定義了序關系后,元素之間就可以按照序關系排序。但就集合本身的特性而言,元素之間沒有必然的序。
空集的性質:空集是一切集合的子集
Set不保存重復的元素。Set中最常被使用的是測試歸屬性,你可以很容易的詢問某個對象是否在某個Set中。Set具有與Collection完全一樣的接口,因此沒有任何額外的功能。實際上Set就是Collection,只是行為不同。
實現了Set接口的主要有HashSet、TreeSet、LinkedHashSet這幾個共同點就是每個相同的項只保存一份。他們也有不同點,區別如下:
1.HashSet:
HashSet使用的是相當復雜的方式來存儲元素的,使用HashSet能夠最快的獲取集合中的元素,效率非常高(以空間換時間)。會根據hashcode和equals來龐端是否是同一個對象,如果hashcode一樣,并且equals返回true,則是同一個對象,不能重復存放。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
package cn.set; import java.util.HashSet; import java.util.Set; class Student{ int id; public Student( int id) { this .id = id; } @Override public String toString() { return this .id+ "" ; } @Override public int hashCode() { return this .id; } @Override public boolean equals(Object obj) { if (obj instanceof Student){ Student stu = (Student) obj; if (stu.id == this .id) return true ; } return false ; } } public class HashSetTest { public static void main(String[] args) { Set<Student> set = new HashSet<Student>(); Student s1 = new Student( 1 ); Student s2 = new Student( 1 ); Student s3 = new Student( 2 ); set.add(s1); set.add(s2); set.add(s3); for (Student s : set) { System.out.println(s); } } } |
正如上例所示,重寫了hashCode()和equals()方法來區分同意對象后,就不能存放同以對象了。如果注釋這兩個方法,則所有Student對象視為不同對象,都可以存放。
2.TreeSet
TreeSet也不能存放重復對象,但是TreeSet會自動排序,如果存放的對象不能排序則會報錯,所以存放的對象必須指定排序規則。排序規則包括自然排序和客戶排序。
①自然排序:TreeSet要添加哪個對象就在哪個對象類上面實現java.lang.Comparable接口,并且重寫comparaTo()方法,返回0則表示是同一個對象,否則為不同對象。
②客戶排序:建立一個第三方類并實現java.util.Comparator接口。并重寫方法。定義集合形式為TreeSet ts = new TreeSet(new 第三方類());
下面一個例子用TreeSet存放自然排序的對象:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
package cn.set; import java.util.Set; import java.util.TreeSet; class Student1 implements Comparable<Student1>{ int id; public Student1( int id) { this .id = id; } @Override public String toString() { return this .id+ "" ; } @Override public int hashCode() { return this .id; } @Override public boolean equals(Object obj) { if (obj instanceof Student1){ Student1 stu = (Student1) obj; if (stu.id == this .id) return true ; } return false ; } public int compareTo(Student1 o) { return ( this .id-o.id); } } public class TreeSetTest { public static void main(String[] args) { Set<Student1> set = new TreeSet<Student1>(); Student1 s1 = new Student1( 5 ); Student1 s2 = new Student1( 1 ); Student1 s3 = new Student1( 2 ); Student1 s4 = new Student1( 4 ); Student1 s5 = new Student1( 3 ); set.add(s1); set.add(s2); set.add(s3); set.add(s4); set.add(s5); for (Student1 s : set) { System.out.println(s); } } } |
輸出結果為:
下面一個例子用TreeSet存放客戶排序的對象:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
package com.set; import java.util.Set; import java.util.TreeSet; class Student1 implements Comparable<Student1>{ int id; public Student1( int id) { this .id = id; } @Override public String toString() { return this .id+ "" ; } @Override public int hashCode() { return this .id; } @Override public boolean equals(Object obj) { if (obj instanceof Student1){ Student1 stu = (Student1) obj; if (stu.id == this .id) return true ; } return false ; } public int compareTo(Student1 o) { return ( this .id-o.id); } } public class TreeSetTest { public static void main(String[] args) { Set<Student1> set = new TreeSet<Student1>(); Student1 s1 = new Student1( 5 ); Student1 s2 = new Student1( 1 ); Student1 s3 = new Student1( 2 ); Student1 s4 = new Student1( 4 ); Student1 s5 = new Student1( 3 ); set.add(s1); set.add(s2); set.add(s3); set.add(s4); set.add(s5); for (Student1 s : set) { System.out.println(s); } } } |
輸出結果為:
大家都知道List存放時按照插入順序排序的,其實也可以用自然排序和客戶排序對List集合排序,大家請看:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
package cn.set; import java.util.ArrayList; import java.util.Collections; import java.util.List; class MySort1 implements java.util.Comparator<Student3>{ public int compare(Student3 o1, Student3 o2) { return o2.id-o1.id; } } class Student3 implements Comparable<Student3>{ int id; public Student3( int id) { this .id = id; } @Override public String toString() { return this .id+ "" ; } public int compareTo(Student3 o) { return ( this .id-o.id); } } public class ListSort { public static void main(String[] args) { List<Student3> list = new ArrayList<Student3>(); Student3 s1 = new Student3( 5 ); Student3 s2 = new Student3( 1 ); Student3 s3 = new Student3( 2 ); Student3 s4 = new Student3( 4 ); Student3 s5 = new Student3( 3 ); list.add(s1); list.add(s2); list.add(s3); list.add(s4); list.add(s5); System.out.println(list); //自然排序: Collections.sort(list); System.out.println(list); //客戶排序 Collections.sort(list, new MySort1()); System.out.println(list); } } |
輸出結果為:
[5, 1, 2, 4, 3]
[1, 2, 3, 4, 5]
[5, 4, 3, 2, 1]
下面為大家介紹Java中的Set集合接口實現插入對象不重復的原理:
在java的集合中,判斷兩個對象是否相等的規則是:
1)、判斷兩個對象的hashCode是否相等
如果不相等,認為兩個對象也不相等,完畢
如果相等,轉入2)
(這一點只是為了提高存儲效率而要求的,其實理論上沒有也可以,但如果沒有,實際使用時效率會大大降低,所以我們這里將其做為必需的。后面會重點講到這個問題。)
2)、判斷兩個對象用equals運算是否相等
如果不相等,認為兩個對象也不相等
如果相等,認為兩個對象相等(equals()是判斷兩個對象是否相等的關鍵)
對于一般類的對象(除String等封裝類型對象外):
若普通類沒有重寫hashcode()和equals()方法,,那么其對象在比較時,是繼承的object類中的hashcode()方法,object類中的hashcode()方法是一個本地方法,對該方法的返回值進行比較時,比較的是對象的地址(引用地址),使用new方法創建內容相同的對象,兩次生成的當然是不同的對象。除非重寫hashcode()方法。在object類中定義的equals()方法也是對對象地址的比較。一句話總結:若不重寫普通類的hashcode()和equals()方法,在Set集合中對象引用地址不一樣,對象即不重復。
對于String等對象(String、Integer、Double····等等):
由于這些封裝類本身已經重寫了hashcode()方法,并且重寫的方法的返回值跟對象的內容相關,而不是跟引用地址相關。這些封裝類中的equals()方法同樣進行了重寫,比較的是對象的內容,而非引用地址。一句話總結:String等類的對象在集合中均比較他們的內容,內容相同則覆蓋已存在的對象。
以上就是本文的全部內容,希望對大家的學習有所幫助。