Working With HashMap Class in Java

Introduction

 
In this article, we are going to describe the HasMap Class's functionality in Java. The HashMap is also part of the collection framework. And this class is in the java.util package of Java. The HashMap class uses a hash table to implement the Map interface. This allows the execution time of the basic operations, such as to get() and put(), to remain constant even for large sets.
 
A Map is storage that holds key to value mappings. There cannot be duplicate keys in a Map and each key maps to at most one value. HashMap is unsynchronized and Hashtable is synchronized. HashMap is the faster one of the two. If you need to maintain map keys in an ordered fashion, you can use TreeMap.
 
The following figure shows where and for what use and the benefits of a hash map.
 
HashMap in Java 
 

What is Hashing

 
Hashing means using a function or algorithm to map object data to some representative integer value. This so-called hash code (or simply hash) can then be used as a way to narrow down our search when looking for the item in the map.
 
Constructor Details
  • HashMap( ) 
  • HashMap(Map m)
  • HashMap(int capacity)
  • HashMap(int capacity, float fillRatio)
The first form constructs a default hash map. And the initial capacity is 16 and the default load factor (0.75). The second form initializes the hash map by using the elements of m. The third form initializes the capacity of the hash map to capacity but default load factor. The fourth form initializes both the capacity and fills ratio of the hash map by using its arguments. The meaning of capacity and fill ratio is the same as for HashSet, described earlier.
 

How you Iterate the value of HasMap?

  1. import java.util.Collection;  
  2. import java.util.HashMap;  
  3. import java.util.Iterator;  
  4. public class HashMapDemo  
  5. {  
  6.     public static void main(String[] args)  
  7.     {  
  8.         HashMap < String, String > hMp = new HashMap < String, String > ();  
  9.         hMp.put("1""Abhishek");  
  10.         hMp.put("2""vineet");  
  11.         hMp.put("3""Akshay");  
  12.         Collection c = hMp.values();  
  13.         Iterator itr = c.iterator();  
  14.         while (itr.hasNext())  
  15.         {  
  16.             System.out.println(itr.next());  
  17.         }  
  18.     }  
  19. }  
Output
 
HashMap in Java
 
Some Operation with HashMap
  1. import java.util.Hashtable;  
  2. public class HashMapOperation  
  3. {  
  4.     public static void main(String[] s)  
  5.     {  
  6.         Hashtable table = new Hashtable();  
  7.         table.put("k1""abhishek");  
  8.         table.put("k2""vineet");  
  9.         table.put("k3""akshay");  
  10.         System.out.println("Size of HashMap after addition : " + table.size());  
  11.         Object obj = table.remove("k2");  
  12.         System.out.println("Size of HashMap after remove one element : " + table.size());  
  13.         System.out.println(obj + " Removed from HashMap");  
  14.         System.out.println(" Is exist " + table.containsKey("k3"));  
  15.         table.clear();  
  16.         System.out.println("after remove all element the table is" + table);  
  17.     }  
  18. }  
Output
 
 HashMap in Java

Copy One hash table to another

  1. import java.util.Hashtable;  
  2. public class CopyDemo  
  3. {  
  4.     public static void main(String[] s)  
  5.     {  
  6.         Hashtable table1 = new Hashtable();  
  7.         table1.put("k1""abhishek");  
  8.         table1.put("k2""vineet");  
  9.         table1.put("k3""deepak");  
  10.         System.out.println(" first table is \n" + table1 + "\n");  
  11.         Hashtable table2 = new Hashtable();  
  12.         table2.put("k4""amit");  
  13.         table2.put("k5""anuj");  
  14.         table2.put("k6""omji");  
  15.         System.out.println("Second table is \n" + table2 + "\n");  
  16.         table2.putAll(table1);  
  17.         System.out.println("after copy the second first table with in second \n");  
  18.         System.out.println(table2);  
  19.     }  
  20. }  
Output
 
 HashMap in Java
 
Resources


Similar Articles