import java.util.Hashtable; public class HashTableApplication { public static void main(String args[]) { Hashtable<String, Integer> hashtable = new Hashtable<String, Integer>(); hashtable.put("ABA", 13); hashtable.put("CAB", 12); hashtable.put("BAC", 15); System.out.println(hashtable); int i = hashtable.get("ABA"); System.out.println("ABAs age is "+i); int j = hashtable.get("BAC"); System.out.println("BACs age is "+j); } }
Luckily, java already provides 'Hashtable' defined in 'java.util.Hashtable'.
All we have done is imported it and used it.
As we know we have stored the names 'ABA', 'CAB' and 'BAC' and their corresponding ages 13, 12 and 15 in the 'Hashtable'.
But how they are stored in the 'HashTable' will be decided by java. As we are using the 'Hashtable' provided by java.
So, for 'ABA', a 'Hash Code' is calculated by java and the age of 'ABA' is stored in some location decided by java.
Similarly, the age of 'CAB' and 'BAC' is stored in the 'Hashtable' after java calculates the 'Hash Code', and decides where they will be stored.
After all the values are stored, we then check all the values stored in the 'Hashtable'.
Then we try to retrieve the age of 'ABA'.
So, we pass the name of 'ABA' and internally java calculates the 'Hash Code' for 'ABA' and gives us the age of 'ABA' from the 'Hashtable'.
And we print the age of 'ABA' on the screen.
Similarly we retrieve the age of 'BAC' and print it on the screen.