Easy Tutorial
For Competitive Exams

Java Programming HashMap

What is HashMap?

HashMap is a Hash table based implementation of the Map interface.

This implementation provides all of the optional map operations, and permits null values and the null key.

The HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls.

This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.

How to perform Basic Operations in HashMap?

The Basic Operations are:

  • creating object
  • adding entries
  • getting values by passing key
  • checking is hashmap is empty or not
  • deleting an element and size of the HashMap

Basic Operations

import java.util.HashMap;
 
public class MyBasicHashMap {
 
    public static void main(String a[]){
        HashMap hm = new HashMap();
        hm.put("first", "FIRST INSERTED");
        hm.put("second", "SECOND INSERTED");
        hm.put("third","THIRD INSERTED");
        System.out.println(hm);
        System.out.println("Value of second: "+hm.get("second"));
        System.out.println("Is HashMap empty? "+hm.isEmpty());
        hm.remove("third");
        System.out.println(hm);
        System.out.println("Size of the HashMap: "+hm.size());
    }
}
Try it Yourself

How to iterate through HashMap?

The method keySet() returns all key entries as a set object.

Iterating through each key, we can get corresponding value object.

Example

import java.util.HashMap;
import java.util.Set;
public class MyHashMapRead {
    public static void main(String a[]){
        HashMap hm = new HashMap();
        hm.put("first", "FIRST INSERTED");
        hm.put("second", "SECOND INSERTED");
        hm.put("third","THIRD INSERTED");
        System.out.println(hm);
        Set keys = hm.keySet();
        for(String key: keys){
            System.out.println("Value of "+key+" is: "+hm.get(key));
        }
    }
}
Try it Yourself

How to copy Map content to another HashMap?

putAll() method helps us to copy another collections to HashMap object.

Example

import java.util.HashMap;
 
public class MyHashMapCopy {
 
    public static void main(String a[]){
        HashMap hm = new HashMap();
        hm.put("first", "FIRST INSERTED");
        hm.put("second", "SECOND INSERTED");
        hm.put("third","THIRD INSERTED");
        System.out.println(hm);
        HashMap subMap = new HashMap();
        subMap.put("s1", "S1 VALUE");
        subMap.put("s2", "S2 VALUE");
        hm.putAll(subMap);
        System.out.println(hm);
    }
}
Try it Yourself

How to delete all elements from HashMap?

By calling clear() method, we can delete all key-value pairs from HashMap.

Example

import java.util.HashMap;
public class MyHashMapClear {
    public static void main(String a[]){
        HashMap hm = new HashMap();
        hm.put("first", "FIRST INSERTED");
        hm.put("second", "SECOND INSERTED");
        hm.put("third","THIRD INSERTED");
        System.out.println("My HashMap content:");
        System.out.println(hm);
        System.out.println("Clearing HashMap:");
        hm.clear();
        System.out.println("Content After clear:");
        System.out.println(hm);
    }
}
Try it Yourself

How to search a key in HashMap?

By using containsKey() method,we can find out the key existence.

Example

import java.util.HashMap;
public class KeySearch {
  public static void main(String a[]){
        HashMap hm = new HashMap();
        hm.put("first", "FIRST INSERTED");
        hm.put("second", "SECOND INSERTED");
        hm.put("third","THIRD INSERTED");
        System.out.println(hm);
        if(hm.containsKey("first")){
            System.out.println("The hashmap contains key first");
        } else {
            System.out.println("The hashmap does not contains key first");
        }
        if(hm.containsKey("fifth")){
            System.out.println("The hashmap contains key fifth");
        } else {
            System.out.println("The hashmap does not contains key fifth");
        }
    }
}
Try it Yourself

How to search a value in HashMap?

By using containsValue() method, we can find out the value existence.

Example

import java.util.HashMap;
public class ValueSearch {
     public static void main(String a[]){
        HashMap hm = new HashMap();
        hm.put("first", "FIRST INSERTED");
        hm.put("second", "SECOND INSERTED");
        hm.put("third","THIRD INSERTED");
        System.out.println(hm);
        if(hm.containsValue("SECOND INSERTED")){
            System.out.println("The hashmap contains value SECOND INSERTED");
        } else {
            System.out.println("The hashmap does not contains value SECOND INSERTED");
        }
        if(hm.containsValue("first")){
            System.out.println("The hashmap contains value first");
        } else {
            System.out.println("The hashmap does not contains value first");
        }
    }
}
Try it Yourself

How to get all keys from HashMap?

By calling keySet() method, we can get set object with all key values.

Example

import java.util.HashMap;
import java.util.Set;
public class MapKeys {
     public static void main(String a[]){
        HashMap hm = new HashMap();
        //add key-value pair to hashmap
        hm.put("first", "FIRST INSERTED");
        hm.put("second", "SECOND INSERTED");
        hm.put("third","THIRD INSERTED");
        System.out.println(hm);
        Set keys = hm.keySet();
        for(String key: keys){
            System.out.println(key);
        }
    }
}
Try it Yourself

How to get entry set from HashMap?

Entry class provides getter methods to access key-value details. The method entrySet() provides all entries as set object.

Example

import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;
 
public class Demo {
 
    public static void main(String a[]){
        HashMap hm = new HashMap();
        hm.put("first", "FIRST INSERTED");
        hm.put("second", "SECOND INSERTED");
        hm.put("third","THIRD INSERTED");
        System.out.println(hm);
        Set> entires = hm.entrySet();
        for(Entry ent:entires){
            System.out.println(ent.getKey()+" = "+ent.getValue());
        }
    }
}
Try it Yourself

Solve this!!

#20814,20815,20816,20817
Share with Friends