更新时间:2022-10-27 10:03:53 来源:极悦 浏览916次
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年,晋升架构
提交申请后,顾问老师会电话与您沟通安排学习