Set接口
Set接口和List接口一樣,繼承Collection接口,Set接口中元素無序,並且都會以某種規則保證存入的元素不出現重複。
HashSet集合
HashSet是Set介面的實作類,所儲存的元素是不可重複的,且元素都是無序的,當在HashSet集合中新增一個物件時,首先會呼叫該物件的hashCode()方法來計算物件的雜湊值,從而確定元素的儲存位置。如果此時雜湊值相同,再呼叫物件的equals()方法來確保該位置沒有重複元素。
package 集合类; import java.util.HashSet; import java.util.Iterator; public class Set { public static void main(String[] args) { HashSet set=new HashSet(); set.add("hello"); set.add("world"); set.add("abc"); set.add("hello"); Iterator it=set.iterator(); while(it.hasNext()){ Object obj=it.next(); System.out.print(obj+ " "); } } }
運行結果
由運行結果可以看出,取出元素的順序和添加元素的順序並不一致,並且重複的字符串被去掉了,只加了一次,是因為HashSet集合的add()方法存入元素時,首先調用當前存入對象的hashCode()方法獲得對象的哈希值,然後根據哈希值算出一個存儲位置,如果這個位置上沒有元素,則直接將該元素存入,如果該位置上有元素存在,則會呼叫equal()方法讓目前存入的元素依序和該位置上的元素比較。如果傳回結果為false就將該元素存入集合,傳回結果為true,則表示有重複元素,將該元素捨棄。
package 集合类; import java.util.HashSet; class Student{ String id; String name; public Student(String id,String name){ this.id=id; this.name=name; } public String toString(){ String s = id + ":" + name; return s; } } public class Set1 { public static void main(String[] args) { HashSet hs=new HashSet(); Student stu1=new Student("1","hello"); Student stu2=new Student("2","world"); Student stu3=new Student("1","hello"); hs.add(stu1); hs.add(stu2); hs.add(stu3); System.out.println(hs); } }
運行結果
所沒有去除重複的元素,是因為定義Student類別時沒有重寫hashCode()和equals()方法。
package API; import java.util.HashSet; class Student{ private String id; private String name; public Student(String id,String name){ this.id=id; this.name=name; } //重写toString方法 public String toString(){ return id+ ":"+name; } //重写hashCode方法 public int hashCode(){ //返回id属性的哈希值 return id.hashCode(); } public boolean equals(Object obj){ //判断是否是同一个对象 if(this==obj){ return true; } //判断对象是Student类型 if(!(obj instanceof Student)){ return false; } //将对象强转为Student类型 Student stu=(Student) obj; //判断id是否相同 boolean b=this.id.equals(stu.id); return b; } } public class Set2 { public static void main(String[] args) { HashSet hs=new HashSet(); Student stu1=new Student("1","hello"); Student stu2=new Student("2","world"); Student stu3=new Student("1","hello"); hs.add(stu1); hs.add(stu2); hs.add(stu3); System.out.println(hs); } }
運行結果
由於Student類別重寫了Object類別的hashCode()和equals()方法。在hashCode()方法中傳回id屬性的雜湊值,在equals()方法中比較物件的id屬性是否相等,並傳回結果。當呼叫HashSet集合的add()方法加入stu3物件時,發現它的雜湊值與stu2物件相同,而且stu2.equals(stu3)回傳true。 HashSet認定兩個物件相同,因此重複的Student物件被移除了。
以上是Java怎麼使用Set介面儲存沒有重複元素的陣列的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本文討論了使用Maven和Gradle進行Java項目管理,構建自動化和依賴性解決方案,以比較其方法和優化策略。

本文使用Maven和Gradle之類的工具討論了具有適當的版本控制和依賴關係管理的自定義Java庫(JAR文件)的創建和使用。

本文討論了使用咖啡因和Guava緩存在Java中實施多層緩存以提高應用程序性能。它涵蓋設置,集成和績效優勢,以及配置和驅逐政策管理最佳PRA

本文討論了使用JPA進行對象相關映射,並具有高級功能,例如緩存和懶惰加載。它涵蓋了設置,實體映射和優化性能的最佳實踐,同時突出潛在的陷阱。[159個字符]

Java的類上載涉及使用帶有引導,擴展程序和應用程序類負載器的分層系統加載,鏈接和初始化類。父代授權模型確保首先加載核心類別,從而影響自定義類LOA


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

Dreamweaver Mac版
視覺化網頁開發工具

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

記事本++7.3.1
好用且免費的程式碼編輯器

Atom編輯器mac版下載
最受歡迎的的開源編輯器

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。