
What is Hashtable in java collection?

Java collection -Hashtable;-
The hashtable is used to store value in the form of map key with value.
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
public class HashTableExample {
public static void main(String [] args){
Map map = new Hashtable();
map.put("A", "Apple");
map.put("B", "Orange");
map.put("C", "Grapes");
map.put("E", "Pine");
map.put("D", "Mango");
Iterator iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry mapEntry = (Map.Entry) iterator.next();
System.out.println("The key is:" + mapEntry.getKey()
+ ", value is :" + mapEntry.getValue());
}
}
}
Output:
The key is:A, value is :Apple The key is:E, value is :Pine The key is:D, value is :Mango The key is:C, value is :Grapes The key is:B, value is :Orange
Description:- The above example demonstrates you the Map interface. Since Map is an interface, you need to instantiate a concrete implementation of the interface in order to use it. Here Map is implemented by its subclass Hashtable. Using the put method, we have added elements to key and value pair. Now to display the key and values, we have called getKey() and getValue() methods that are defined by Map.Entry.
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.