更新時(shí)間:2022-10-27 10:03:53 來(lái)源:動(dòng)力節(jié)點(diǎn) 瀏覽1671次
Hashtable類實(shí)現(xiàn)了一個(gè)哈希表,它將鍵映射到值。任何非空對(duì)象都可以用作鍵或值。要成功地從哈希表中存儲(chǔ)和檢索對(duì)象,用作鍵的對(duì)象必須實(shí)現(xiàn) hashCode 方法和 equals 方法。
它類似于 HashMap,但是是同步的。
Hashtable 將鍵/值對(duì)存儲(chǔ)在哈希表中。
在 Hashtable 中,我們指定一個(gè)用作鍵的對(duì)象,以及我們要與該鍵關(guān)聯(lián)的值。然后對(duì)鍵進(jìn)行哈希處理,生成的哈希碼用作值存儲(chǔ)在表中的索引。
Hashtable 類的初始默認(rèn)容量為 11,而 loadFactor 為 0.75。
HashMap 不提供任何枚舉,而 Hashtable 不提供快速失敗的枚舉。
宣言:
公共類 Hashtable<K,V> 擴(kuò)展 Dictionary<K,V> 實(shí)現(xiàn) Map<K,V>、Cloneable、Serializable
類型參數(shù):
K - 此映射維護(hù)的鍵的類型
V – 映射值的類型

Hashtable 實(shí)現(xiàn)了 Serializable、Cloneable、Map<K,V>接口并擴(kuò)展了Dictionary<K,V>。直接子類是Properties,UIDefaults。
為了創(chuàng)建一個(gè) Hashtable,我們需要從java.util.Hashtable導(dǎo)入它。我們可以通過(guò)多種方式創(chuàng)建 Hashtable。
1. Hashtable():這將創(chuàng)建一個(gè)空的哈希表,默認(rèn)加載因子為 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):這將創(chuàng)建一個(gè)哈希表,其初始大小由 initialCapacity 指定,默認(rèn)加載因子為 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):這個(gè)版本創(chuàng)建一個(gè)哈希表,其初始大小由size指定,填充率由fillRatio指定。填充率:基本上,它決定了哈希表在向上調(diào)整大小之前可以有多滿,其值介于 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):這將創(chuàng)建一個(gè)用 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
相關(guān)閱讀
Java實(shí)驗(yàn)班
0基礎(chǔ) 0學(xué)費(fèi) 15天面授
Java就業(yè)班
有基礎(chǔ) 直達(dá)就業(yè)
Java夜校直播班
業(yè)余時(shí)間 高薪轉(zhuǎn)行
Java在職加薪班
工作1~3年,加薪神器
Java架構(gòu)師班
工作3~5年,晉升架構(gòu)
提交申請(qǐng)后,顧問(wèn)老師會(huì)電話與您溝通安排學(xué)習(xí)