Here we will see Hashtable in details.
- Basic points about Hashtable.
- Different ways to iterate Hashtable in Java.
- Constructors and methods of Hashtable in Java.
Let’s see some points and examples for Hashtable.
- Hashtable is a legacy class which extends Dictionary class and implements Map, Cloneable, Serializable interface.
- Properties, UIDefaults are the child class of Hashtable.
- Hashtable stores the elements in key and value format.
- In Hashtable, the key can’t be duplicate, but the value can be duplicate.
- In Hashtable, null is not allowed as key or value. If we try to add null as a key or value, there is no compilation error but during the execution of the program, we will get NullPointerException.
- Hashtable store the elements in an unordered way.
- Hashtable is synchronized.
Example 1 –
package hashtable; import java.util.Hashtable; import java.util.Iterator; import java.util.Map; public class HashtableExample1 { public static void main(String[] args) { Map<Integer, String> hashtableObject = new Hashtable<>(); hashtableObject.put(2, "ram"); hashtableObject.put(3, "mohan"); hashtableObject.put(4, "shyam"); hashtableObject.put(5, "Rakesh"); // we will iterate using Iterator Iterator<Map.Entry<Integer, String>> iterator = hashtableObject .entrySet().iterator(); while (iterator.hasNext()) { Map.Entry<Integer, String> entry = iterator.next(); System.out.println("Key is = " + entry.getKey() + ", Value is = " + entry.getValue()); } } }
Output is –
Key is = 5, Value is = Rakesh
Key is = 4, Value is = shyam
Key is = 3, Value is = mohan
Key is = 2, Value is = ram
Example 2 – inserting null as key in Hashtable.
package hashtable; import java.util.Hashtable; import java.util.Iterator; import java.util.Map; public class HashtableExample1 { public static void main(String[] args) { Map<Integer, String> hashtableObject = new Hashtable<>(); hashtableObject.put(2, "ram"); hashtableObject.put(3, "mohan"); hashtableObject.put(null, "shyam"); hashtableObject.put(5, "Rakesh"); // we will iterate hashtable using for each loop for (Integer key : hashtableObject.keySet()) { System.out.println("Key is : " + key + " Value is : " + hashtableObject.get(key)); } } }
Output is –
Exception in thread “main” java.lang.NullPointerException
at java.util.Hashtable.put(Unknown Source)
at hashtable.HashtableExample1.main(HashtableExample1.java:13)
Example 3 – Adding null as value.
package hashtable; import java.util.Hashtable; import java.util.Iterator; import java.util.Map; public class HashtableExample1 { public static void main(String[] args) { Map<Integer, String> hashtableObject = new Hashtable<>(); hashtableObject.put(2, "ram"); hashtableObject.put(3, "mohan"); hashtableObject.put(4, null); hashtableObject.put(5, "Rakesh"); // we will iterate hashtable using for each loop for (Integer key : hashtableObject.keySet()) { System.out.println("Key is : " + key + " Value is : " + hashtableObject.get(key)); } } }
Output is –
Exception in thread “main” java.lang.NullPointerException
at java.util.Hashtable.put(Unknown Source)
at hashtable.HashtableExample1.main(HashtableExample1.java:13)
You may like –
HashMap in java.
TreeMap in java.
LinkedHashMap in Java.
TreeSet in Java.
TreeSet in Java.
LinkedList in Java.
Vector in Java.
ArrayList in Java.
How to make List, Set and Map Read Only in Java.
How to avoid duplicate elements in ArrayList.
Adding Custom type class object in ArrayList.
How to Synchronize ArrayList in Java.
Different ways to iterate ArrayList in java.
How to convert Arraylist to array and array to ArrayList in Java.
Difference between Arrays and Collections Framework in Java.
How ArrayList works internally in Java.
User defined class as key in HashMap in Java.
Fail fast and fail safe example in java.