HashMap in java


We will cover following points of HashMap in Java:

    1. Basic Points about HashMap.
    2. Different ways to iterate HashMap in Java.
    3. Constructors and methods of HashMap in Java.

Basics of HashMap in Java

  1. HashMap is a class which extends AbstractMap class and implements Map, Cloneable, Serializable interface.
  2. HashMap is used to store the element in the form of key and value format.
  3. HashMap, the key cannot be duplicate key, the value can be duplicate.
  4. HashMap can have null ad key or value.
  5. HashMap stores the elements in an unordered way.
  6. HashMap element can be accessed randomly.
  7. Hashmap none of the methods are synchronized so the HashMap class is also not synchronized.
  8. HashMap objects are eligible for Serialization and Cloning.
  9. HashMap element can’t be accessed by using Enumeration and ListIterator directly.
  10. In order to access the elements of HashMap, we can access on the basis of values or on the basis of the key.

Different ways to iterate HashMap in Java

Using Iterator – Iterating HashMap using an iterator.

We will use HashMap’s entrySet() method which returns a Set type of Map.Entry, something like (Set<Map.Entry<K,V>>).

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

class HashMapExample {
      public static void main(String[] arg) {
              Map<Integer, String> hashmapObject = new HashMap<>();

                                    hashmapObject.put(2, "Nadeem");
                                    hashmapObject.put(3, "Puneet");
                                    hashmapObject.put(4, "Shyam");
                                    hashmapObject.put(5, "Sagar");

                                    // we will iterate using Iterator
                                    Iterator<Map.Entry<Integer, String>> iterator = hashmapObject
                                     .entrySet().iterator();

                                     while (iterator.hasNext()) {
                                        Map.Entry<Integer, String> entry = iterator.next();
                                           System.out.println("Key is = " + entry.getKey() + ", Value is = "
                                       + entry.getValue());
                                      }
             }
}
Using for each loop -Iterating HashMap using for each loop.

First use entrySet() method then for each loop.

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class HashMapExample1 {
             public static void main(String[] args) {
                    Map<Integer, String> hashMapObj = new HashMap<>();

                                         hashMapObj.put(2, "nadeem");
                                         hashMapObj.put(5, "puneet");
                                         hashMapObj.put(3, "shyam");

                                         Set<Map.Entry<Integer, String>> entrySet = hashMapObj.entrySet();

                                         for (Map.Entry<Integer, String> entry : entrySet) {
                                             System.out.println("Key is : " + entry.getKey() + " Value is : "
                                             + entry.getValue());
                                          }
          }
}

Using Lambda expression – Iterating HashMap using lambda expression.

We can also iterate HashMap using lambda expression as below.

import java.util.HashMap;
import java.util.Map;

public class HashMapExample1 {
      public static void main(String[] args) {
         Map<Integer, String> hashMapObj = new HashMap<>();

           hashMapObj.put(2, "Shyam");
           hashMapObj.put(5, "Puneet");
           hashMapObj.put(3, "Surya");
           hashMapObj.put(9, "Sagar");

           hashMapObj.forEach((k, v) -> {
           System.out.println("Key is : " + k + " Value is : " + v);
           });

      }
}

Using java 8 stream() method –

import java.util.Map;
import java.util.Set;

public class HashMapExample1 {
        public static void main(String[] args) {
           Map<Integer, String> hashMapObj = new HashMap<>();

               hashMapObj.put(2, "Puneet");
               hashMapObj.put(5, "Shyam");
               hashMapObj.put(3, "Surya");
               hashMapObj.put(9, "Sagar");

          Set<Map.Entry<Integer, String>> setReference= hashMapObj.entrySet();

             setReference.stream().forEach
             (mapEntry-> System.out.println("Key is : "+mapEntry.getKey()+" Value is :                                                                                       "+mapEntry.getValue()));
        }
}

Constructors and Methods of HashMap in Java

  • HashMap()
  • HashMap(int initialCapacity)
  • HashMap(int initialCapacity, float loadFactor)
  • HashMap(Map map)

 


%d bloggers like this: