Java HashMap

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Java HashMap class

Java HashMap class hierarchy

Java HashMap class implements the map interface by using a hashtable. It inherits AbstractMap class and implements Map interface.

The important points about Java HashMap class are:

  • A HashMap contains values based on the key.

  • It contains only unique elements.

  • It may have one null key and multiple null values.

  • It maintains no order.

Hierarchy of HashMap class

As shown in the above figure, HashMap class extends AbstractMap class and implements Map interface.

HashMap class declaration

Let's see the declaration for java.util.HashMap class.

  1. public class HashMap extends AbstractMap implements Map, Cloneable, Serializable  

HashMap class Parameters

Let's see the Parameters for java.util.HashMap class.

  • K: It is the type of keys maintained by this map.

  • V: It is the type of mapped values.

Constructors of Java HashMap class

Constructor

Description

HashMap()

It is used to construct a default HashMap.

HashMap(Map m)

It is used to initializes the hash map by using the elements of the given Map object m.

HashMap(int capacity)

It is used to initializes the capacity of the hash map to the given integer value, capacity.

HashMap(int capacity, float fillRatio)

It is used to initialize both the capacity and fill ratio of the hash map by using its arguments.

Methods of Java HashMap class

Method

Description

void clear()

It is used to remove all of the mappings from this map.

boolean containsKey(Object key)

It is used to return true if this map contains a mapping for the specified key.

boolean containsValue(Object value)

It is used to return true if this map maps one or more keys to the specified value.

boolean isEmpty()

It is used to return true if this map contains no key-value mappings.

Object clone()

It is used to return a shallow copy of this HashMap instance: the keys and values themselves are not cloned.

Set entrySet()

It is used to return a collection view of the mappings contained in this map.

Set keySet()

It is used to return a set view of the keys contained in this map.

Object put(Object key, Object value)

It is used to associate the specified value with the specified key in this map.

int size()

It is used to return the number of key-value mappings in this map.

Collection values()

It is used to return a collection view of the values contained in this map.

Java HashMap Example

  1. mport java.util.*;  
  2. class TestCollection13{  
  3.  public static void main(String args[]){  
  4.   HashMap<Integer,String> hm=new HashMap<Integer,String>();  
  5.   hm.put(100,"Amit");  
  6.   hm.put(101,"Vijay");  
  7.   hm.put(102,"Rahul");  
  8.   for(Map.Entry m:hm.entrySet()){  
  9.    System.out.println(m.getKey()+" "+m.getValue());  
  10.   }  
  11.  }  
  12. }  

Test it Now

Output:102 Rahul
       100 Amit
       101 Vijay

Java HashMap Example: remove()

  1. import java.util.*;  
  2. public class HashMapExample {  
  3.    public static void main(String args[]) {  
  4.    // create and populate hash map  
  5.    HashMap<Integer, String> map = new HashMap<Integer, String>();           
  6.    map.put(101,"Let us C");  
  7.    map.put(102"Operating System");  
  8.    map.put(103"Data Communication and Networking");  
  9.    System.out.println("Values before remove: "+ map);    
  10.    // Remove value for key 102  
  11.    map.remove(102);  
  12.    System.out.println("Values after remove: "+ map);  
  13.    }      
  14. }  

Output:

Values before remove: {102=Operating System, 103=Data Communication and Networking, 101=Let us C}
Values after remove: {103=Data Communication and Networking, 101=Let us C}

Difference between HashSet and HashMap

HashSet contains only values whereas HashMap contains entry(key and value).

Java HashMap Example: Book

  1. import java.util.*;    
  2. class Book {    
  3. int id;    
  4. String name,author,publisher;    
  5. int quantity;    
  6. public Book(int id, String name, String author, String publisher, int quantity) {    
  7.     this.id = id;    
  8.     this.name = name;    
  9.     this.author = author;    
  10.     this.publisher = publisher;    
  11.     this.quantity = quantity;    
  12. }    
  13. }    
  14. public class MapExample {    
  15. public static void main(String[] args) {    
  16.     //Creating map of Books    
  17.     Map<Integer,Book> map=new HashMap<Integer,Book>();    
  18.     //Creating Books    
  19.     Book b1=new Book(101,"Let us C","Yashwant Kanetkar","BPB",8);    
  20.     Book b2=new Book(102,"Data Communications & Networking","Forouzan","Mc Graw Hill",4);    
  21.     Book b3=new Book(103,"Operating System","Galvin","Wiley",6);    
  22.     //Adding Books to map   
  23.     map.put(1,b1);  
  24.     map.put(2,b2);  
  25.     map.put(3,b3);  
  26.       
  27.     //Traversing map  
  28.     for(Map.Entry<Integer, Book> entry:map.entrySet()){    
  29.         int key=entry.getKey();  
  30.         Book b=entry.getValue();  
  31.         System.out.println(key+" Details:");  
  32.         System.out.println(b.id+" "+b.name+" "+b.author+" "+b.publisher+" "+b.quantity);   
  33.     }    
  34. }    
  35. }    

Output:

1 Details:
101 Let us C Yashwant Kanetkar BPB 8
2 Details:
102 Data Communications & Networking Forouzan Mc Graw Hill 4
3 Details:
103 Operating System Galvin Wiley 6