In Java, Hashtable is used to store key-value pairs that map each and every key to certain values. It is synchronized, unlike the HashMap. Moreover, Hashtable does not allow null values or null keys and contains unique elements as well. As already said, the hash table contains a key that is hashed and obtain a hash code. Then, this code will be used as an index where the particular value will be stored.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
Declaration of Hashtable:
Hashtable class can be declared using the below syntax.
public class Hashtable<Key,Value> extends Dictionary<Key,Value> implements Map<Key,Value>, Cloneable, Serializable
Hashtable can be created using both parameterized and non-parameterized constructors.
The following are the commonly used methods in HashTable.
Each data structure has its own special features.
Below are the examples of implementing hashtable in java.
Java program to add keys and values to the hashtable.
Code:
//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 ")); } }
Output:
Values of A, B, C, D, and E will be displayed on executing the code. Moreover, the new balance of A will also be displayed, as shown below.
Java program to remove keys and values from the hashtable.
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.
The above is the detailed content of Hashtable in Java. For more information, please follow other related articles on the PHP Chinese website!