How to Add,remove, and get specific key and value from HashTable.
In this program we created a Hashtable and add some value with specific keys,after we check size of Hashtable,and remove some value with specific key.
import java.util.Hashtable;
import java.util.Set;
public class HashtableIteration {
public static void main(String a[]){
//Create hashtable instance
Hashtable ht = new Hashtable();
//add key-value pair to hashtable
ht.put("key1", "value1");
ht.put("key2", "value2");
ht.put("key3","value3");
System.out.println("Size of Hashtable :"+ht.size());
//getting value for the given key from hashtable
System.out.println("Value of key 'key2': "+ht.get("key2"));
System.out.println("Is Hashtable empty? "+ht.isEmpty());
ht.remove("key3");
System.out.println(ht);
System.out.println("Size of the Hashtable: "+ht.size());
}
}
Size of Hashtable :3
Value of key 'key2': value2
Is Hashtable empty? false
{key2=value2, key1=value1}
Size of the Hashtable: 2




post a comment