wellnwill Marathahalli Bangalore

HashMap

  • HashMap is implemented based on hash table.
  • The Objects will be inserted based on keys that is why insertion order is not preserved.
  • Keys should be unique.
  • Values can be duplicated.
  • We can insert heterogeneous elements as key and value.
  • HashMap class implements Serializable and Cloneable interface but it doesnot implement RandomAccess interface.
  • HashMap is basically used for search operation.
  • The default capacity is 16.
  • Multiple threads can access HashMap object.
  • HashMap class is non syncronized . If we have a requirement to make HashMap class as sychronized then use synchronizedMap() method of collections class.
  • Searching and insertion is fast.
  • Both key and value can be null.
import java.util.*;
class Test278
{
 public static void main(String args[])
 {
  HashMap hm=new HashMap();
  hm.put("001","John");
  hm.put("002","Robert");
  hm.put("003","Mac");
  hm.put("004","David");
  System.out.println(hm);
  System.out.println(hm.size());
  System.out.println(hm.isEmpty());
  System.out.println(hm.containsKey("100"));
  System.out.println(hm.containsValue("Dav"));
  System.out.println(hm.get("004"));
  
   

  Set sk=hm.keySet();
  Iterator ktr=sk.iterator();
  System.out.println("----Keys----");
  while(ktr.hasNext())
  {
   System.out.println(ktr.next());
  }
Collection c=hm.values();
  Iterator vtr=c.iterator();
  System.out.println("----Values----");
  while(vtr.hasNext())
  {
   System.out.println(vtr.next());
  }
 }
}


 
import java.util.*;
class Test279
{
 public static void main(String args[])
 {
  HashMap hm=new HashMap();
  hm.put("009","John");
  hm.put("002","Robert");
  hm.put("008","Mac");
  hm.put("004","David");
  System.out.println(hm);

  Set s=hm.entrySet();
  Iterator str=s.iterator();
  System.out.println("----travesing----");
 

while(str.hasNext())
  {
   Map.Entry e=(Map.Entry)str.next();
   System.out.println("Keys:"+e.getKey()+"Values:"
   +e.getValue());
   
   if(e.getKey().equals("004"))
   {
    e.setValue("Bob");
   }
  }
  System.out.println(hm);
 }
}




How to get synchronized Map object.

                                                       
import java.util.*;
class SynMap
{
public static void main(String args[])
{
HashMap hm1=new HashMap(50);
Map hm2=Collections.synchronizedMap(hm1);
}
}
Note: hm1 is non-synchronized and hm2 is synchronized.


Frequency of each element in an array

import java.util.HashMap;
 
public class A6 
{  
    static void elementCount(int a[])
    {
        //Creating a HashMap object with array elements as keys and their count as value
         
        HashMap hm = new HashMap();
         
        //checking every element of array
         
        for (int i : a) 
        {
            if(hm.containsKey(i))
            {
                //If element is present in hm, increment count by 1
                 
                hm.put(i, hm.get(i)+1);
            }
            else
            {
                //If element is not present in hm,adding this element to hm with value as 1
                hm.put(i, 1);
            }
        }
         
        System.out.println("count : "+hm);
    }
     
    public static void main(String[] args) 
    {    
	  int a[]={1,1,2,2,3,3,4};
         for(int i=0;i< a.length;i++)
	 {
		  System.out.print(a[i]+",");
	 }
         System.out.println();
         elementCount(a);
    }    
}

Higher frequency element in an array

import java.util.HashMap;
import java.util.*;
  
public class A7
{    
    static void higherFreqencyNumber(int a[])
    {
        //Creating HashMap object with elements as keys and their occurrences as values
          
        HashMap hm = new HashMap();
          
        //checking every element of array and inserting into hm object
          
        for (int i : a) 
        {
            if (hm.containsKey(i))
            {
                //If element is present in hm, increment count by 1                  
                hm.put(i, hm.get(i)+1);
            }
            else
            {
                            //If element is not present in hm,adding this element to hm with value as 1
                  
                hm.put(i, 1);
            }
        }
          
        int element = 0;
          
        int occurrence = 1;
          
        //traversing hm object to get the most frequent element and its occurrences
          
        Set> s=hm.entrySet();
        Iterator str=s.iterator();  
       
		while(str.hasNext())
		 {
		   Map.Entry e=(Map.Entry)str.next();
		   System.out.println("Keys:"+e.getKey()+" "+"Values:"+e.getValue());
		   
		   if(e.getValue() > occurrence)
                   {
                   element = e.getKey();
                  
                   occurrence = e.getValue();
                   }
		 }
          
          
        if(occurrence > 1)
        {
              
            System.out.println("Most occurrence element : "+element);
              
            System.out.println("Frequency : "+occurrence);
              
        }
        else
        {
            System.out.println("no occurrence.");
        }
    }
      
    public static void main(String[] args)
    {
        int a[]={1,2,2,3,5,5,5};

	for(int i=0; i< a.length;i++)
	{
         System.out.print(a[i]+",");
        }
        System.out.println();
        higherFreqencyNumber(a);
    }
}
Footer with Map