Java教程-Java Hashtable类

Java Hashtable类
Java Hashtable类实现了一个哈希表,用于将键映射到值。它继承了Dictionary类并实现了Map接口。
要记住的要点
- Hashtable是一个数组的列表。每个列表称为一个桶(bucket)。通过调用hashcode()方法来确定桶的位置。Hashtable根据键来存储值。
- Java Hashtable类包含唯一的元素。
- Java Hashtable类不允许空键或空值。
- Java Hashtable类是同步的。
- Hashtable类的初始默认容量为11,负载因子为0.75。
Hashtable类声明
让我们看一下java.util.Hashtable类的声明。
public class Hashtable<K, V> extends Dictionary<K, V> implements Map<K, V>, Cloneable, Serializable
Hashtable类参数
让我们看一下java.util.Hashtable类的参数。
- K:此映射维护的键的类型。
- V:此映射将键映射到的值的类型。
Java Hashtable示例
import java.util.*;
class HashtableExample {
public static void main(String args[]) {
Hashtable<Integer, String> hm = new Hashtable<Integer, String>();
hm.put(100, "Amit");
hm.put(102, "Ravi");
hm.put(101, "Vijay");
hm.put(103, "Rahul");
for (Map.Entry m : hm.entrySet()) {
System.out.println(m.getKey() + " " + m.getValue());
}
}
}
输出:
103 Rahul
102 Ravi
101 Vijay
100 Amit
Java Hashtable示例:remove()
import java.util.*;
public class HashtableExample {
public static void main(String args[]) {
Hashtable<Integer, String> map = new Hashtable<Integer, String>();
map.put(100, "Amit");
map.put(102, "Ravi");
map.put(101, "Vijay");
map.put(103, "Rahul");
System.out.println("Before remove: " + map);
// 移除键为102的值
map.remove(102);
System.out.println("After remove: " + map);
}
}
输出:
Before remove: {103=Rahul, 102=Ravi, 101=Vijay, 100=Amit}
After remove: {103=Rahul, 101=Vijay, 100=Amit}
Java Hashtable示例:getOrDefault()
import java.util.*;
class HashtableExample {
public static void main(String args[]) {
Hashtable<Integer, String> map = new Hashtable<Integer, String>();
map.put(100, "Amit");
map.put(102, "Ravi");
map.put(101, "Vijay");
map.put(103, "Rahul");
// 在getOrDefault方法中,我们指定了if和else语句作为参数
System.out.println(map.getOrDefault(101, "Not Found"));
System.out.println(map.getOrDefault(105, "Not Found"));
}
}
输出:
Vijay
Not Found
Java Hashtable示例:putIfAbsent()
import java.util.*;
class HashtableExample {
public static void main(String args[]) {
Hashtable<Integer, String> map = new Hashtable<Integer, String>();
map.put(100, "Amit");
map.put(102, "Ravi");
map.put(101, "Vijay");
map.put(103, "Rahul");
System.out.println("Initial Map: " + map);
// 插入,因为指定的键值对是唯一的
map.putIfAbsent(104, "Gaurav");
System.out.println("Updated Map: " + map);
// 返回当前值,因为指定的键值对已经存在
map.putIfAbsent(101, "Vijay");
System.out.println("Updated Map: " + map);
}
}
输出:
Initial Map: {103=Rahul, 102=Ravi, 101=Vijay, 100=Amit}
Updated Map: {104=Gaurav, 103=Rahul, 102=Ravi, 101=Vijay, 100=Amit}
Updated Map: {104=Gaurav, 103=Rahul, 102=Ravi, 101=Vijay, 100=Amit}
Java Hashtable示例:Book
import java.util.*;
class Book {
int id;
String name, author, publisher;
int quantity;
public Book(int id, String name, String author, String publisher, int quantity) {
this.id = id;
this.name = name;
this.author = author;
this.publisher = publisher;
this.quantity = quantity;
}
}
public class HashtableExample {
public static void main(String[] args) {
// 创建一个存储Book对象的映射
Map<Integer, Book> map = new Hashtable<Integer, Book>();
// 创建Book对象
Book b1 = new Book(101, "Let us C", "Yashwant Kanetkar", "BPB", 8);
Book b2 = new Book(102, "Data Communications & Networking", "Forouzan", "Mc Graw Hill", 4);
Book b3 = new Book(103, "Operating System", "Galvin", "Wiley", 6);
// 将Book对象添加到映射中
map.put(1, b1);
map.put(2, b2);
map.put(3, b3);
// 遍历映射
for (Map.Entry<Integer, Book> entry : map.entrySet()) {
int key = entry.getKey();
Book b = entry.getValue();
System.out.println(key + " Details:");
System.out.println(b.id + " " + b.name + " " + b.author + " " + b.publisher + " " + b.quantity);
}
}
}
输出:
3 Details:
103 Operating System Galvin Wiley 6
2 Details:
102 Data Communications & Networking Forouzan Mc Graw Hill 4
1 Details:
101 Let us C Yashwant Kanetkar BPB 8