首頁  >  文章  >  Java  >  Java 中的哈希集

Java 中的哈希集

WBOY
WBOY原創
2024-08-30 16:20:45804瀏覽

set介面由HashSet類別實現,該類別有一個哈希表作為備份,是HashMap的一個實例。儘管如此,此類並不能保證元素隨時間的順序。此 HashSet 類別允許空元素,為刪除、新增等操作提供時間效能。假設元素透過雜湊函數分散在儲存桶中。

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

文法

public class HashSet<E> extends AbstractSet<E> implements Set<E>, Cloneable, Serializable

Java HashSet 中的建構子

java HashSet 類別由多個建構子組成。他們是:

  • HashSet(): 可以使用此建構子 HashSet() 建構預設的 HashSet。
  • HashSet(intcapacity):可以使用此建構子 HashSet(intcapacity) 將雜湊集的容量初始化為給定整數的容量。當我們繼續在哈希集中添加元素時,哈希集的容量會自動增長。
  • HashSet(int capacity, float loadFactor): 雜湊集的容量可以初始化為給定整數的容量,並使用此建構子 HashSet( int 容量,浮點數 loadFactor)。
  • HashSet(Collection Extends E>c): 使用此建構子將集合 c 的元素初始化為雜湊集 HashSet(Collection Extends E>c)

Java HashSet 的實作方法

java HashSet 類別由幾個方法組成。他們是:

  • add(E e): 如果使用此方法 Add(E e) 集合中不存在特定元素,則會將特定元素新增至集合。
  • clear(): 使用此方法 Clear() 刪除集合中的所有元素。
  • clone():使用此方法 Clone() 傳回雜湊集實例的淺表副本。元素本身無法克隆。
  • contains(Object o):如果集合中存在指定元素,則此方法 Contains(object o) 傳回 true。
  • isEmpty():如果集合中沒有元素,則此方法 isEmpty() 傳回 true。
  • iterator(): 使用此方法 Iterator() 傳回集合元素上的迭代器。
  • remove(Object o):如果集合中存在指定元素,則此方法Remove(object o) 會刪除該指定元素。
  • size(): 使用此方法 Size() 傳回集合中元素的數量。
  • spliterator(): 元素上的後期綁定和快速 spliterator 是使用此方法 Spliterator() 建立的集合。

在 Java 中實作 HashSet 的範例

以下是用Java實作HashSet的範例:

範例#1

建立一個雜湊集並將新元素加入到建立的新集合中。

代碼:

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);
}
}

輸出:

Java 中的哈希集

範例#2

集合並示範 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 中的哈希集

範例#3

用於示範雜湊集操作的 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 中的哈希集

範例#4

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);
}
}

輸出:

Java 中的哈希集

結論

在本教程中,我們了解雜湊集的概念,包括雜湊集的定義、建立雜湊集的語法、雜湊集的建構函式、雜湊集的方法以及建立雜湊集的程式範例、為新建立的雜湊集新增元素hashset,從現有的hashset 中刪除元素,檢查hashset 中的元素。

以上是Java 中的哈希集的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
上一篇:Java 哈希碼()下一篇:Java 哈希碼()