在 Java 中,哈希表用于存储将每个键映射到特定值的键值对。与 HashMap 不同,它是同步的。此外,Hashtable 不允许空值或空键,并且也包含唯一元素。正如已经说过的,哈希表包含一个经过哈希处理并获得哈希码的键。然后,此代码将用作存储特定值的索引。
开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
哈希表声明:
Hashtable 类可以使用以下语法声明。
public class Hashtable<Key,Value> extends Dictionary<Key,Value> implements Map<Key,Value>, Cloneable, Serializable
可以使用参数化和非参数化构造函数创建哈希表。
以下是HashTable中常用的方法
每种数据结构都有自己的特殊功能。
下面是用java实现哈希表的例子。
用于将键和值添加到哈希表的 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的新余额也会显示出来,如下图。
用于从哈希表中删除键和值的 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中文网其他相关文章!