set介面由HashSet類別實現,該類別有一個哈希表作為備份,是HashMap的一個實例。儘管如此,此類並不能保證元素隨時間的順序。此 HashSet 類別允許空元素,為刪除、新增等操作提供時間效能。假設元素透過雜湊函數分散在儲存桶中。
開始您的免費軟體開發課程
網頁開發、程式語言、軟體測試及其他
文法
public class HashSet<E> extends AbstractSet<E> implements Set<E>, Cloneable, Serializable
java HashSet 類別由多個建構子組成。他們是:
java HashSet 類別由幾個方法組成。他們是:
以下是用Java實作HashSet的範例:
建立一個雜湊集並將新元素加入到建立的新集合中。
代碼:
import java.util.HashSet; import java.util.Set; //A class Example is created public class Example { public static void main(String[] args) { // A hashset is created Set<String> months = new HashSet<>(); // New elements are added to the hashset months.add("January"); months.add("Febraury"); months.add("March"); months.add("April"); months.add("May"); months.add("June"); months.add("July"); months.add("August"); months.add("September"); months.add("October"); months.add("November"); months.add("December"); System.out.println(months); } }
輸出:
集合並示範 Hashset(collection c) 建構子的使用。
代碼:
import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; //A class Example is created public class Example { public static void main(String[] args) { // A hashset is created List<Integer> Divby4 = new ArrayList<>(); Divby4.add(4); Divby4.add(8); Divby4.add(12); Divby4.add(16); Divby4.add(20); List<Integer> Divby2 = new ArrayList<>(); Divby2.add(2); Divby2.add(4); Divby2.add(6); Divby2.add(8); Divby2.add(10); // A hashset is created from another collection Divby4 Set<Integer> Divby4Or2 = new HashSet<>(Divby4); // Adding the elements of divby2 to the existing hashset Divby4Or2.addAll(Divby2); System.out.println(Divby4Or2); } }
輸出:
用於示範雜湊集操作的 Java 程序,例如檢查雜湊集是否為空、檢查雜湊集中元素的數量以及檢查雜湊集中是否存在元素。
代碼:
import java.util.HashSet; import java.util.Set; //A class Example is created public class Example { public static void main(String[] args) { // A hashset is created Set<String> rivers = new HashSet<>(); // performing isempty operation on the set to check if it is empty System.out.println("Are there no elements in rivers set? : " + rivers.isEmpty()); rivers.add("Kaveri"); rivers.add("Ganga"); rivers.add("Yamuna"); rivers.add("Godavari"); // Checking the size of the hashset System.out.println("The count of rivers in the hashset are " + rivers.size()); // checking if an element is present in the hashset String Name = "Ganga"; if(rivers.contains(Name)) { System.out.println(Name + " is present in the rivers hashset."); } else { System.out.println(Name + " is not present in the rivers hashset."); } } }
輸出:
Java 程式從雜湊集中刪除一個元素,刪除屬於不同集合的所有元素,從雜湊集中刪除滿足特定條件的元素以及從雜湊集中刪除所有元素。
代碼:
import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; //A class Example is created public class Example { public static void main(String[] args) { // A hashset is created Set<Integer> num = new HashSet<>(); num.add(2); num.add(3); num.add(4); num.add(5); num.add(6); num.add(7); num.add(8); num.add(9); num.add(10); System.out.println("Numbers added to the hashset are : " + num); // An element from the hashset is removed. False is returned if that element doesnt exists in the hashset boolean Remove = num.remove(5); System.out.println("After remove the number 5 from the hashset, the remaining elemnts are => " + num); // all the elements that belong to a different collection are removed from the hashset List<Integer> Squares = new ArrayList<>(); Squares.add(4); Squares.add(9); num.removeAll(Squares); System.out.println("After removing all the elements that belong to a different collection => " + num); // Elements matching a certain condition is removed from the hashset num.removeIf(num1 -> num1 % 2 == 0); System.out.println("After removing all the elements matching a certain condition is removed from the hashset => " + num); // Clearing the hashset num.clear(); System.out.println("After clearing the hashset => " + num); } }
輸出:
在本教程中,我們了解雜湊集的概念,包括雜湊集的定義、建立雜湊集的語法、雜湊集的建構函式、雜湊集的方法以及建立雜湊集的程式範例、為新建立的雜湊集新增元素hashset,從現有的hashset 中刪除元素,檢查hashset 中的元素。
以上是Java 中的哈希集的詳細內容。更多資訊請關注PHP中文網其他相關文章!