Monday 17 October 2016

java.util.HashMap class

java.util.HashMap class

java.util.HashMap class
  • We can store group of objects in the form of key-value pairs in HashMap 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.
  • HashMap is non synchronized.
  • Order is no guarantee.
--------------------------------------------------------------------------------------------
Program         :       Example on HashMap class
Program name  :       HMDemo1.java
Output            :         
      {1=Nireekshan, 2=Sravan, 3=Hari, 9=Mohan, 10=Mouli}
     
import java.util.HashMap;

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

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