Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   
   HTML   
   CSS   
   JAVA SCRIPT   
   JQUERY   
   HTML   
   CSS   
   JAVA SCRIPT   
   JQUERY   




HASH TABLE CODE



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);
    }
}


Output :



  {CAB=12, ABA=13, BAC=15}
  ABAs age is 13
  BACs age is 15


Code Explanation for Hash Table


Luckily, java already provides 'Hashtable' defined in 'java.util.Hashtable'.


All we have done is imported it and used it.


import java.util.Hashtable;

As we know we have stored the names 'ABA', 'CAB' and 'BAC' and their corresponding ages 13, 12 and 15 in the 'Hashtable'.


hashtable.put("ABA", 13);
hashtable.put("CAB", 12);
hashtable.put("BAC", 15);

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'.


System.out.println(hashtable);

Then we try to retrieve the age of 'ABA'.


int i = hashtable.get("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.


System.out.println("ABAs age is "+i);

Similarly we retrieve the age of 'BAC' and print it on the screen.


int j = hashtable.get("BAC");

System.out.println("BACs age is "+j);