Tuesday, 18 October 2016

java.util.Hashtable class

java.util.Hashtable class

java.util.Hashtable class
  • We can store group of objects in the form of key-value pairs in Hashtable object.
  • Both key and values are objects only.
  • Keys can be unique and values can be duplicate.
  • Here group of objects means, it can allow us to store same type of objects and different type of objects.
    • Same type means, assuming that all objects are in the form of integer types.
    • Different type means, assuming that all objects are in the form of integer, float, String, Student and Customer types also.
  • Size will increase dynamically.
  • Hashtable is synchronized.
--------------------------------------------------------------------------------------------
Program         :       Example on Hashtable class
Program name  :       HTDemo1.java
Output            :         
      {10=Mouli, 9=Mohan, 3=Hari, 2=Sravan, 1=Nireekshan}
     
import java.util.Hashtable;

class HTDemo1
{
       public static void main(String args[])
       {
             Hashtable hashtable= new Hashtable();
              
             hashtable.put(1, "Nireekshan");
             hashtable.put(2, "Sravan");
             hashtable.put(10, "Mouli");
             hashtable.put(9, "Mohan");
             hashtable.put(3, "Hari");
             
             System.out.println(hashtable);
       }
}

Compile       :         javac HTDemo1.java
Run             :         java HTDemo1
Output        :         
     {10=Mouli, 9=Mohan, 3=Hari, 2=Sravan, 1=Nireekshan}
--------------------------------------------------------------------------------------------
Thanks for your time.
Nireekshan