LinkedHashMap in Java


We will cover following points of LinkedHasMap in Java:

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

Basics of LinkedHasMap in Java

  1. LinkedHasMap is a class which extends LinkedHasMap and implements Map interface.
  2. LinkedHasMap uses node representation to store the elements.
  3. LinkedHasMap stores the elements in key and value pair.
  4. The key of LinkedHasMap can’t be duplicate whereas value can be duplicate.
  5. We can have null as key and value in LinkedHasMap.
  6. LinkedHasMap maintains insertion order which makes this differ from LinkedHasMap.
  7. The initial capacity and load factor is 16 and 0.75 for LinkedHasMap.
  8. LinkedHasMap methods are not synchronized.

Different ways to iterate LinkedHasMap in Java

Using Iterator – Iterating LinkedHasMap using an iterator.

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

import java.util.LinkedHasMap;
import java.util.Iterator;
import java.util.Map;

class LinkedHasMapExample {
      public static void main(String[] arg) {
              Map<Integer, String> linkedHasMapObject = new LinkedHasMap<>();

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

                                    // we will iterate using Iterator
                        Iterator<Map.Entry<Integer, String>> iterator = linkedHasMapObject
                         .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 LinkedHasMap using for each loop.

First use entrySet() method then for each loop.

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

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

                           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 LinkedHasMap using lambda expression.

We can also iterate LinkedHasMap using lambda expression as below.

import java.util.LinkedHasMap;
import java.util.Map;

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

           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 LinkedHasMapExample1 {
        public static void main(String[] args) {
           Map<Integer, String> hashMapObj = new LinkedHasMap<>();

               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 LinkedHasMap in Java

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

 


%d bloggers like this: