更新時間:2022-10-27 10:03:53 來源:動力節點 瀏覽1240次
Hashtable類實現了一個哈希表,它將鍵映射到值。任何非空對象都可以用作鍵或值。要成功地從哈希表中存儲和檢索對象,用作鍵的對象必須實現 hashCode 方法和 equals 方法。
它類似于 HashMap,但是是同步的。
Hashtable 將鍵/值對存儲在哈希表中。
在 Hashtable 中,我們指定一個用作鍵的對象,以及我們要與該鍵關聯的值。然后對鍵進行哈希處理,生成的哈希碼用作值存儲在表中的索引。
Hashtable 類的初始默認容量為 11,而 loadFactor 為 0.75。
HashMap 不提供任何枚舉,而 Hashtable 不提供快速失敗的枚舉。
宣言:
公共類 Hashtable<K,V> 擴展 Dictionary<K,V> 實現 Map<K,V>、Cloneable、Serializable
類型參數:
K - 此映射維護的鍵的類型
V – 映射值的類型
Hashtable 實現了 Serializable、Cloneable、Map<K,V>接口并擴展了Dictionary<K,V>。直接子類是Properties,UIDefaults。
為了創建一個 Hashtable,我們需要從java.util.Hashtable導入它。我們可以通過多種方式創建 Hashtable。
1. Hashtable():這將創建一個空的哈希表,默認加載因子為 0.75,初始容量為 11。
Hashtable<K, V> ht = new Hashtable<K, V>();
// Java program to demonstrate
// adding elements to Hashtable
import java.io.*;
import java.util.*;
class AddElementsToHashtable {
public static void main(String args[])
{
// No need to mention the
// Generic type twice
Hashtable<Integer, String> ht1 = new Hashtable<>();
// Initialization of a Hashtable
// using Generics
Hashtable<Integer, String> ht2
= new Hashtable<Integer, String>();
// Inserting the Elements
// using put() method
ht1.put(1, "one");
ht1.put(2, "two");
ht1.put(3, "three");
ht2.put(4, "four");
ht2.put(5, "five");
ht2.put(6, "six");
// Print mappings to the console
System.out.println("Mappings of ht1 : " + ht1);
System.out.println("Mappings of ht2 : " + ht2);
}
}
輸出
ht1 的映射:{3=三,2=二,1=一}
ht2 的映射:{6=6, 5=5, 4=4}
2. Hashtable(int initialCapacity):這將創建一個哈希表,其初始大小由 initialCapacity 指定,默認加載因子為 0.75。
Hashtable<K, V> ht = new Hashtable<K, V>(int initialCapacity);
// Java program to demonstrate
// adding elements to Hashtable
import java.io.*;
import java.util.*;
class AddElementsToHashtable {
public static void main(String args[])
{
// No need to mention the
// Generic type twice
Hashtable<Integer, String> ht1 = new Hashtable<>(4);
// Initialization of a Hashtable
// using Generics
Hashtable<Integer, String> ht2
= new Hashtable<Integer, String>(2);
// Inserting the Elements
// using put() method
ht1.put(1, "one");
ht1.put(2, "two");
ht1.put(3, "three");
ht2.put(4, "four");
ht2.put(5, "five");
ht2.put(6, "six");
// Print mappings to the console
System.out.println("Mappings of ht1 : " + ht1);
System.out.println("Mappings of ht2 : " + ht2);
}
}
輸出
ht1 的映射:{3=三,2=二,1=一}
ht2 的映射:{4=4, 6=6, 5=5}
3. Hashtable(int size, float fillRatio):這個版本創建一個哈希表,其初始大小由size指定,填充率由fillRatio指定。填充率:基本上,它決定了哈希表在向上調整大小之前可以有多滿,其值介于 0.0 到 1.0 之間。
Hashtable<K, V> ht = new Hashtable<K, V>(int size, float fillRatio);
// Java program to demonstrate
// adding elements to Hashtable
import java.io.*;
import java.util.*;
class AddElementsToHashtable {
public static void main(String args[])
{
// No need to mention the
// Generic type twice
Hashtable<Integer, String> ht1
= new Hashtable<>(4, 0.75f);
// Initialization of a Hashtable
// using Generics
Hashtable<Integer, String> ht2
= new Hashtable<Integer, String>(3, 0.5f);
// Inserting the Elements
// using put() method
ht1.put(1, "one");
ht1.put(2, "two");
ht1.put(3, "three");
ht2.put(4, "four");
ht2.put(5, "five");
ht2.put(6, "six");
// Print mappings to the console
System.out.println("Mappings of ht1 : " + ht1);
System.out.println("Mappings of ht2 : " + ht2);
}
}
輸出
ht1 的映射:{3=三,2=二,1=一}
ht2 的映射:{6=6, 5=5, 4=4}
4. Hashtable(Map m):這將創建一個用 m 中的元素初始化的哈希表。
Hashtable<K, V> ht = new Hashtable<K, V>(Map m);
// Java program to demonstrate
// adding elements to Hashtable
import java.io.*;
import java.util.*;
class AddElementsToHashtable {
public static void main(String args[])
{
// No need to mention the
// Generic type twice
Map<Integer, String> hm = new HashMap<>();
// Inserting the Elements
// using put() method
hm.put(1, "one");
hm.put(2, "two");
hm.put(3, "three");
// Initialization of a Hashtable
// using Generics
Hashtable<Integer, String> ht2
= new Hashtable<Integer, String>(hm);
// Print mappings to the console
System.out.println("Mappings of ht2 : " + ht2);
}
}
輸出
ht2 的映射:{3=3,2=2,1=1}
例子:
// Java program to illustrate
// Java.util.Hashtable
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Create an empty Hashtable
Hashtable<String, Integer> ht = new Hashtable<>();
// Add elements to the hashtable
ht.put("vishal", 10);
ht.put("sachin", 30);
ht.put("vaibhav", 20);
// Print size and content
System.out.println("Size of map is:- " + ht.size());
System.out.println(ht);
// Check if a key is present and if
// present, print value
if (ht.containsKey("vishal")) {
Integer a = ht.get("vishal");
System.out.println("value for key"
+ " \"vishal\" is:- " + a);
}
}
}
輸出
地圖大小為:- 3
{vaibhav=20,vishal=10,sachin=30}
鍵“vishal”的值是:- 10
0基礎 0學費 15天面授
有基礎 直達就業
業余時間 高薪轉行
工作1~3年,加薪神器
工作3~5年,晉升架構
提交申請后,顧問老師會電話與您溝通安排學習