Java                                                          Home : www.sharetechnote.com

 

 

 

 

Java - HashMap

 

 

 

 

 

How to create and print a Map ?

 

 

HashMap01.java

import java.io.*;

 

import java.util.*;  

 

public class HashMap01 {

 

    public static void main(String[] args) {

        

        Map<Integer,String> hMap=new HashMap<Integer,String>();  

        

        hMap.put(1001,"Item1");  

        hMap.put(1002,"Item2");  

        hMap.put(1003,"Item3");  

  

        System.out.println(hMap.toString());

        

        for(Map.Entry m:hMap.entrySet()){  

            System.out.println(m.getKey()+","+m.getValue());  

        }  

 

    }

 

}

 

      Output :

       

        {1001=Item1, 1002=Item2, 1003=Item3}  <-- System.out.println(hMap.toString());

        1001,Item1    <-- for()

        1002,Item2

        1003,Item3

 

 

 

How to  pass a Map into a Method ?

 

 

HashMap02.java

import java.util.*;  

 

public class HashMap02 {

 

    public static void main(String[] args) {

        

        Map<Integer,String> hMap=new HashMap<Integer,String>();  

        

        hMap.put(1001,"Item1");  

        hMap.put(1002,"Item2");  

        hMap.put(1003,"Item3");  

  

        printMap(hMap);

 

    }

    

    public static void printMap(Map<Integer,String> hMap)

    {

        System.out.println("\n----------------------------------");

        for(Map.Entry m:hMap.entrySet()){  

            System.out.println(m.getKey()+","+m.getValue());  

        }

        

    }

 

}

 

      Output :

       

        ------------------------------

        1001,Item1

        1002,Item2

        1003,Item3

 

 

 

How to retrieve an Entry from a Map ?

 

 

HashMap03.java

import java.util.*;  

 

public class HashMap03 {

 

    public static void main(String[] args) {

        

        Map<Integer,String> hMap=new HashMap<Integer,String>();  

        

        hMap.put(1001,"Item1");  

        hMap.put(1002,"Item2");  

        hMap.put(1003,"Item3");  

  

        String hme = hMap.get(1002);

 

        System.out.println(hme);

 

    }

     

}

 

      Output :

       

        Item2

         

 

 

 

How to remove an Entry from a Map ?

 

 

HashMap04.java

import java.util.*;  

 

public class HashMap04 {

 

    public static void main(String[] args) {

        

        Map<Integer,String> hMap=new HashMap<Integer,String>();  

        

        hMap.put(1001,"Item1");  

        hMap.put(1002,"Item2");  

        hMap.put(1003,"Item3");  

        System.out.print("Number of Entry = " + hMap.size());

        printMap(hMap);

        

        hMap.remove(1002);

        System.out.print("Number of Entry = " + hMap.size());

        printMap(hMap);

        

        hMap.clear();

        System.out.print("Number of Entry = " + hMap.size());

        printMap(hMap);

 

    }

    

    public static void printMap(Map<Integer,String> hMap)

    {

        System.out.println("\n----------------------------------");

        if(hMap.size() > 0) {

            for(Map.Entry m:hMap.entrySet()){  

                System.out.println(m.getKey()+","+m.getValue());  

            }

            System.out.println("");

        } else {

            System.out.println("No Entry");

        };

        

    }

    

}

 

      Output :

       

        Number of Entry = 3

        ----------------------------------

        1001,Item1

        1002,Item2

        1003,Item3

         

        Number of Entry = 2

        ----------------------------------

        1001,Item1

        1003,Item3

         

        Number of Entry = 0

        ----------------------------------

        No Entry

         

 

 

 

How to replace an Entry in a Map ?

 

 

HashMap05.java

iimport java.util.*;  

 

public class HashMap05 {

 

    public static void main(String[] args) {

        

        Map<Integer,String> hMap=new HashMap<Integer,String>();  

        

        hMap.put(1001,"Item1");  

        hMap.put(1002,"Item2");  

        hMap.put(1003,"Item3");  

        System.out.print("Number of Entry = " + hMap.size());

        printMap(hMap);

        

        hMap.replace(1002,"Updated Item2");

        System.out.print("Number of Entry = " + hMap.size());

        printMap(hMap);

    }

    

    public static void printMap(Map<Integer,String> hMap)

    {

        System.out.println("\n----------------------------------");

        if(hMap.size() > 0) {

            for(Map.Entry m:hMap.entrySet()){  

                System.out.println(m.getKey()+","+m.getValue());  

            }

            System.out.println("");

        } else {

            System.out.println("No Entry");

        };

        

    }

    

}

 

      Output :

       

        Number of Entry = 3

        ----------------------------------

        1001,Item1

        1002,Item2

        1003,Item3

         

        Number of Entry = 3

        ----------------------------------

        1001,Item1

        1002,Updated Item2

        1003,Item3

         

 

 

 

How to check the existence of an Entry in a Map ?

 

 

HashMap06.java

import java.util.*;  

 

public class HashMap06 {

 

    public static void main(String[] args) {

        

        Map<Integer,String> hMap=new HashMap<Integer,String>();  

        

        hMap.put(1001,"Item1");  

        hMap.put(1002,"Item2");  

        hMap.put(1003,"Item3");  

        

        System.out.println(hMap.containsValue(1002));

        System.out.println(hMap.containsValue("Item2"));

        System.out.println(hMap.containsValue(1004));

        System.out.println(hMap.containsValue("Item4"));

        

    }

    

 

}

 

      Output :

       

        false   <-- System.out.println(hMap.containsValue(1002));

        true    <-- System.out.println(hMap.containsValue("Item2"));

        false   <-- System.out.println(hMap.containsValue(1004));

        false   <-- System.out.println(hMap.containsValue("Item4"));

       

 

 

 

How to put an Entry only when the same Entry is not in the Map ?

 

 

HashMap07.java

import java.util.*;  

 

public class HashMap07 {

 

    public static void main(String[] args) {

        

        Map<Integer,String> hMap=new HashMap<Integer,String>();  

        

        hMap.put(1001,"Item1");  

        hMap.put(1002,"Item2");  

        hMap.put(1003,"Item3");  

        System.out.print("Number of Entry = " + hMap.size());

        printMap(hMap);

        

        hMap.putIfAbsent(1002,"New Item");

        System.out.print("Number of Entry = " + hMap.size());

        printMap(hMap);

        

        hMap.putIfAbsent(1004,"New Item");

        System.out.print("Number of Entry = " + hMap.size());

        printMap(hMap);

    }

    

    public static void printMap(Map<Integer,String> hMap)

    {

        System.out.println("\n----------------------------------");

        if(hMap.size() > 0) {

            for(Map.Entry m:hMap.entrySet()){  

                System.out.println(m.getKey()+","+m.getValue());  

            }

            System.out.println("");

        } else {

            System.out.println("No Entry");

        };

        

    }

    

}

 

      Output :

        Number of Entry = 3

        ----------------------------------

        1001,Item1

        1002,Item2

        1003,Item3

         

        Number of Entry = 3  <-- after hMap.putIfAbsent(1002,"New Item");

        ----------------------------------

        1001,Item1

        1002,Item2

        1003,Item3

         

        Number of Entry = 4  <-- after hMap.putIfAbsent(1004,"New Item");

        ----------------------------------

        1001,Item1

        1002,Item2

        1003,Item3

        1004,New Item

         

 

 

 

How to apply a function to every Entry in a Map ?

 

                   NOTE : forEtach() method is supported in Java 8 or later.

HashMap09.java

import java.util.*;  

 

public class HashMap09 {

 

    public static void main(String[] args) {

        

        Map<Integer,String> hMap=new HashMap<Integer,String>();  

        

        hMap.put(1001,"Item1");  

        hMap.put(1002,"Item2");  

        hMap.put(1003,"Item3");  

        System.out.print("Number of Entry = " + hMap.size());

        printMap(hMap);

        

        hMap.forEach((k,v)->

                              {  

                                 String s = v.toUpperCase();

                                 hMap.replace(k,s);

                              }

                    );

        System.out.print("Number of Entry = " + hMap.size());

        printMap(hMap);         

                        

    }

    

    public static void printMap(Map<Integer,String> hMap)

    {

        System.out.println("\n----------------------------------");

        if(hMap.size() > 0) {

            hMap.forEach((k,v)->System.out.println(k + "," + v));

            System.out.println("");

        } else {

            System.out.println("No Entry");

        };

        

    }

    

}

 

      Output :

        Number of Entry = 3

        ----------------------------------

        1001,Item1

        1002,Item2

        1003,Item3

         

        Number of Entry = 3

        ----------------------------------

        1001,ITEM1

        1002,ITEM2

        1003,ITEM3

         

 

 

 

Reference :

 

[1] Class HashMap<K,V> (Oravle)