Set集合接口-HashSet_TreeSet理解


Set集合里面並不存在有get()方法。 Set本身也屬於接口,而在Set接口下有兩個常用的子類:HashSet、TreeSet。

在以后的開發之中,Set集合一定不會作為我們的首選出現。使用最多的依然是List集合。

1.無序存放:HashSet

 1 package cn.demo;
 2 
 3 import java.util.HashSet;
 4 import java.util.Set;
 5 public class TestHash {
 6     public static void main(String[] args) throws Exception {
 7         Set<String> set = new HashSet<String>();
 8         set.add("java");
 9         set.add("html"); 
10         set.add("jsp"); 
11         set.add("null"); 
12         System.out.println(set);
13     }
14 }
15     

結果:

[java, null, jsp, html]

TreeSet有序存放,在TreeSet集合里面無法保存null數據類型。HashSet可以保存null.

2.TreeSet排序說明

既然TreeSet允許實現排序的處理操作,那么下面將采用自定義對象進行排序處理。那么此時對象所在的類一定要實現Comparable接口,否則在使用add()保存數據的時候就會出現“ClassCastException”。但是在使用此類操作時必須保證在compareTo()方法里面將所有的屬性進行比較。

 1 package cn.demo;
 2 
 3 import java.util.Set;
 4 import java.util.TreeSet;
 5 class Mible implements Comparable<Mible>{
 6     private String brand;
 7     private double price; 
 8     public Mible(String brand,double price){
 9         this.brand = brand;
10         this.price = price;
11     }
12     @Override 
13     public String toString() {
14         return "Mible [brand=" + brand + ", price=" + price + "\n";
15     }
16 public int compareTo(Mible o){
17     if(this.price> o.price){
18         return 1;
19     }else if (this.price< o.price){
20         return -1;
21     }else{
22         return this.brand.compareTo(o.brand);
23     }
24  }    
25 }
26 public class Test {
27     public static void main(String[] args) throws Exception {
28         Set<Mible> set = new TreeSet<Mible>();
29         set.add(new Mible("黑米",19.9));
30         set.add(new Mible("黑米",19.9));
31         set.add(new Mible("黑米",19.9));
32         set.add(new Mible("黑米",4343.9));
33         set.add(new Mible("小米",197.9));
34         set.add(new Mible("白米",19.9));
35         
36         System.out.println(set);
37     }    
38 }

結果:

[Mible [brand=白米, price=19.9
, Mible [brand=黑米, price=19.9
, Mible [brand=小米, price=197.9
, Mible [brand=黑米, price=4343.9
]

結論:TreeSet子類判斷重復元素的一句依靠的是compareTo()方法返回的是否為0。

3.關於重復元素的說明

Comparable只能夠針對於排序的子類使用,而着並不是真正的所謂的重復元素的判斷的依據,而在集合之中,對於重復元素的判斷使用的是Object類中兩個方法完成的:

· 取得Hash碼:public int hashCode();

· 對象比較:public boolean equals(Object obj)。

HashCode是一組根據對象中所有屬性的內容自動計算出來的一個不會重復的數值。在進行數據判斷的時候會首先通過HashCode找到存儲的對象,但是只是依靠HashCode還不足以完全可靠,那么還需要進行身份的信息的匹配,那么此時依靠的是equals()方法。

   在Eclipse里面針對於HashCode()和equals()實際上並不需要用戶自己去編寫,可以直接通過工具自動生成:

代碼:

 1 package cn.demo;
 2 
 3 import java.util.HashSet;
 4 import java.util.Set;
 5 class Mible{
 6     private String brand;
 7     private double price; 
 8     public Mible(String brand,double price){
 9         this.brand = brand;
10         this.price = price;
11     }
12     @Override 
13     public String toString() {
14         return "Mible [brand=" + brand + ", price=" + price + "\n";
15     }
16     @Override
17     public int hashCode() {
18         final int prime = 31;
19         int result = 1;
20         result = prime * result + ((brand == null) ? 0 : brand.hashCode());
21         long temp;
22         temp = Double.doubleToLongBits(price);
23         result = prime * result + (int) (temp ^ (temp >>> 32));
24         return result;
25     }
26     @Override
27     public boolean equals(Object obj) {
28         if (this == obj)
29             return true;
30         if (obj == null)
31             return false;
32         if (getClass() != obj.getClass())
33             return false;
34         Mible other = (Mible) obj;
35         if (brand == null) {
36             if (other.brand != null)
37                 return false;
38         } else if (!brand.equals(other.brand))
39             return false;
40         if (Double.doubleToLongBits(price) != Double.doubleToLongBits(other.price))
41             return false;
42         return true;
43     }
44 
45 }
46 public class Test {
47     public static void main(String[] args) throws Exception {
48         Set<Mible> set = new HashSet<Mible>();
49         set.add(new Mible("黑米",19.9));
50         set.add(new Mible("黑米",19.9));
51         set.add(new Mible("黑米",4343.9));
52         set.add(new Mible("小米",197.9));
53         set.add(new Mible("白米",19.9));
54         
55         System.out.println(set);
56     }    
57 }

結果:

[Mible [brand=小米, price=197.9
, Mible [brand=黑米, price=4343.9
, Mible [brand=黑米, price=19.9
, Mible [brand=白米, price=19.9
]

總結:對於重復元素的判斷依靠的是Object類中的hashCode()與equals()方法執行判斷的。以后首選使用HashSet。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM