Set容器——HashSet及常用API,sethashsetapi

來源:互聯網
上載者:User

Set容器——HashSet及常用API,sethashsetapi

Set容器特點:

①   Set容器是一個不包含重複元素的Collection,並且最多包含一個null元素,它和List容器相反,Set容器不能保證其元素的順序;

②   最常用的兩個Set介面的實作類別是HashSet和TreeSet;

 

HashSet及常用API

①   HashSet擴充AbstractSet並且實現Set介面;

②   HashSet使用散列表(又稱雜湊表)進行儲存;

③   構造方法:

a)   HashSet()

b)   HashSet(Collection c)

c)   HashSet(int capacity)

d)   HashSet(int capacity,float fillRatio)

④   HashSet沒有定義任何超過它的父類和介面提供的其它方法;

⑤   散列集合沒有確保其元素的順序,因為散列處理通常不參與排序;

1         HashSet<String> data=new HashSet<String>();2         data.add("張三");3         data.add("李四");4         data.add("jay");5         data.add("jack");6         data.add("jay");7         System.out.println(data);

輸出結果:

[李四, 張三, jay, jack]

此處第二個jay沒有存入;

可以將其列印出來System.out.println(data.add("jay"));,結果顯示第一個為true,第二個為false

編寫一個Student類:

 1 class Student{ 2     private String name; 3     private int age; 4     public Student(String name, int age) { 5         super(); 6         this.name = name; 7         this.age = age; 8     } 9     public String getName() {10         return name;11     }12     public void setName(String name) {13         this.name = name;14     }15     public int getAge() {16         return age;17     }18     public void setAge(int age) {19         this.age = age;20     }21 }

主方法中添加及輸出

1         HashSet<Student> stuSet=new HashSet<Student>();2         System.out.println(stuSet.add(new Student("張三",20)));3         System.out.println(stuSet.add(new Student("李四",30)));4         System.out.println(stuSet.add(new Student("張三",20)));5         System.out.println(stuSet.size());6     

輸出結果:

true

true

true

3

 

由此可見:new Student("張三",20)兩次都建立了,若想字相同時只建立一次則需重構hashCode和equals方法

如下:

 1     @Override 2     public int hashCode() { 3         final int prime = 31; 4         int result = 1; 5         result = prime * result + age; 6         result = prime * result + ((name == null) ? 0 : name.hashCode()); 7         return result; 8     } 9     @Override10     public boolean equals(Object obj) {11         if (this == obj)12             return true;13         if (obj == null)14             return false;15         if (getClass() != obj.getClass())16             return false;17         Student other = (Student) obj;18         if (age != other.age)19             return false;20         if (name == null) {21             if (other.name != null)22                 return false;23         } else if (!name.equals(other.name))24             return false;25         return true;26     }

再次執行,輸出結果:

true

true

false

2

總結:HashSet的內部操作的底層資料是HashMap,只是我們操作的是HashMap的key;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.