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

Java 中的哈希表

WBOY
WBOY原創
2024-08-30 15:47:42991瀏覽

在 Java 中,雜湊表用於儲存將每個鍵對應到特定值的鍵值對。與 HashMap 不同,它是同步的。此外,Hashtable 不允許空值或空鍵,也包含唯一元素。正如已經說過的,哈希表包含一個經過哈希處理並獲得哈希碼的鍵。然後,此程式碼將用作儲存特定值的索引。

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

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

雜湊表聲明:

Hashtable 類別可以使用以下語法宣告。

public class Hashtable<Key,Value> extends Dictionary<Key,Value> implements Map<Key,Value>, Cloneable, Serializable

雜湊表的建構子

可以使用參數化和非參數化建構函式建立雜湊表。

  • Hashtable():將建構一個新的空雜湊表,負載因子為 0.75,初始容量為 11。這裡初始容量和負載因子是預設值。
  • 雜湊表(int initCapacity):將使用上述初始容量和負載因子 0.75 建立新的空雜湊表。這裡,只有負載因子是預設的。
  • 雜湊表(int initCapacity,float loadfact):將使用上述初始容量和負載因子來建立新的空哈希表。
  • 雜湊表(Map extends Key,? extends Value>t):將使用與上述映射相同的映射建構一個新的雜湊表。

雜湊表的方法

以下是HashTable常用的方法

  • clear(): 此雜湊表將被清除,沒有剩餘鍵。
  • contains(Object val ): 此方法測試是否有任何鍵對應到雜湊表中的值 val。
  • 複製(): 將為雜湊表建立淺表副本。
  • 包含Key(Object k ): 此方法測試指定的鍵 k 在雜湊表中是否可用。
  • 包含一個值(物件val ):如果雜湊表將1 個或多個鍵對應到提到的值val,則將傳回true。
  • 元素(): 將傳回值的列舉。
  • keys(): 將回傳鍵的列舉。
  • entrySet(): 將為地圖上存在的對應傳回一個集合視圖。
  • 等於(物件o):地圖將與指定物件進行比較。
  • get(Objectkey):如果指定的鍵對應到任何值,則會傳回。如果沒有映射任何內容,將傳回 null。
  • isEmpty(): 此方法檢查雜湊表是否未將鍵對應到任何值。
  • keySet(): 將為地圖中存在的鍵傳回集合視圖。
  • size(): 將傳回雜湊表中鍵的數量。
  • hashCode(): 將為地圖傳回雜湊程式碼值。
  • 輸入K 鍵,Val 值:
  • V
  • al 值: k 將會被對應到哈希表中的值val。 putAll(地圖 擴充 K 擴充V
  • al> t): 
  • 對應將從上述對應複製到雜湊表。 刪除(物件
  • k): 
  • 鍵k和對應的值將從表中刪除。
  • ():
將為地圖中存在的值傳回集合視圖。

用 Java 實作雜湊表的範例

每種資料結構都有自己的特殊功能。

下面是用java實作雜湊表的範例。

範例#1

用於將鍵和值新增至雜湊表的 Java 程式。

代碼:
//Java program to add keys and values to the hashtable
import java.util.Enumeration;
import java.util.Hashtable;
//class
public class HashTableExample {
//main method
public static void main(String args[]) {
// Hashtable creation
Hashtable htbl = new Hashtable();
//create an enumeration enm
Enumeration enm;
//create sing s
String s;
//create a double variable balance
double balance;
//add keys and values to the table
htbl.put(" A ", new Double(3500.50));
htbl.put(" B ", new Double(2900.00));
htbl.put(" C ", new Double(3600.00));
htbl.put(" D ", new Double(4550.50));
htbl.put(" E ", new Double(2345.67));
// Store all the keys in the enumeration enm
enm = htbl.keys();
//if more elements are present in the enm, enter this loop
while(enm.hasMoreElements()) {
s = (String) enm.nextElement();
System.out.println(s + ": " + htbl.get(s));
}
System.out.println();
// Add 1000 to value of Key A
balance = ((Double)htbl.get(" A ")).doubleValue();
htbl.put(" A ", new Double(balance + 1000));
System.out.println(" A's new balance : " + htbl.get(" A "));
}
}

輸出:

執行程式碼時將顯示 A、B、C、D 和 E 的值。而A的新餘額也會顯示出來,如下圖。 Java 中的哈希表

範例#2

用於從雜湊表中刪除鍵和值的 Java 程式。

Code:

//Java program to remove keys and values from the hashtable
import java.util.Enumeration;
import java.util.Hashtable;
//class
public class HashTableExample {
//main method
public static void main(String args[]) {
// Hashtable creation
Hashtable<Integer,String> htbl = new Hashtable<Integer,String>();
//add keys and values to the table
htbl.put(1,"29");
htbl.put(2,"30");
htbl.put(3,"31");
htbl.put(4,"32");
htbl.put(5,"33");
htbl.put(6,"34");
htbl.put(7,"35");
System.out.println("Hashtable before removing values: "+ htbl);
// Remove 6 and 3
htbl.remove(6);
htbl.remove(3);
System.out.println("Hashtable after removing values : "+ htbl);
}
}

Output:

Java 中的哈希表

In this program, values of 1, 2, 3, 4, 5, 6 and 7 will be displayed on executing the code. Then the values of 6 and 3 will be removed and display the rest of the values.

Example #3

Java program to get keys and values from the hashtable.

Code:

//Java program to get keys and values from the hashtable
import java.util.Enumeration;
import java.util.Hashtable;
//class
public class HashTableExample {
//main method
public static void main(String args[]) {
// Hashtable creation
Hashtable<Integer,String> htbl = new Hashtable<Integer,String>();
//add keys and values to the table
htbl.put(1,"29");
htbl.put(2,"30");
htbl.put(3,"31");
htbl.put(4,"32");
htbl.put(5,"33");
htbl.put(6,"34");
htbl.put(7,"35");
System.out.println("Hashtable : "+ htbl);
//if value of 3 is present, then return it else print Null
System.out.println(htbl.getOrDefault(3, "Null"));
//if value of 8 is present, then return it else print Null
System.out.println(htbl.getOrDefault(8, "Null"));
}
}

Output:

Java 中的哈希表

In this program also, values of 1, 2, 3, 4, 5, 6 and 7 will be displayed on executing the code. Then, the values for keys 3 and 8 will be retrieved using the method getOrDefault(). Since the value of 8 is not available, null will be returned in the second getOrDefault() method.

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

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