在 Java 中,雜湊表用於儲存將每個鍵對應到特定值的鍵值對。與 HashMap 不同,它是同步的。此外,Hashtable 不允許空值或空鍵,也包含唯一元素。正如已經說過的,哈希表包含一個經過哈希處理並獲得哈希碼的鍵。然後,此程式碼將用作儲存特定值的索引。
開始您的免費軟體開發課程
網頁開發、程式語言、軟體測試及其他
雜湊表聲明:
Hashtable 類別可以使用以下語法宣告。
public class Hashtable<Key,Value> extends Dictionary<Key,Value> implements Map<Key,Value>, Cloneable, Serializable
可以使用參數化和非參數化建構函式建立雜湊表。
以下是HashTable常用的方法
用 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的新餘額也會顯示出來,如下圖。
範例#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:
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.
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:
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中文網其他相關文章!