Skip to content
Related Articles

Related Articles

Improve Article
How to Check if LinkedHashMap is Empty in Java?
  • Last Updated : 11 Dec, 2020

The LinkedHashMap is just like HashMap with an additional feature of maintaining an order of elements inserted into it. HashMap provided the advantage of quick insertion, search, and deletion but it never maintained the track and order of insertion which the LinkedHashMap provides where the elements can be accessed in their insertion order. Here, Let’s illustrate the different approaches for checking if the LinkedHashMap is empty or not.

Example:

Input : {3=Geeks, 2=For, 1=Geeks} 
Output: Given LinkedHashMap is not empty
 
Input : {} 
Output: Given LinkedHashMap is empty

1. Using the size() method :

  1. Create variable lhmSize.
  2. Store the value of the size of LinkedHashMap in it.
  3. If the size of the given LinkedHashMap is 0 then it is empty.
  4. Else it is not empty.

Below is the implementation of the above approach:

Java




// Java Program check if LinkedHashMap is empty or not
import java.util.LinkedHashMap;
public class Main {
  
    public static void main(String[] args)
    {
        // create an instance of LinkedHashMap
        LinkedHashMap<Integer, String> hm1
            = new LinkedHashMap<Integer, String>();
  
        //  Using size() method to make comparison
        System.out.println(
            "Given LinkedHashMap is "
            + (hm1.size() == 0 ? "empty" : "not empty"));
  
        // Add mappings using put method
        hm1.put(3, "Geeks");
        hm1.put(2, "For");
        hm1.put(1, "Geeks");
        int lhmSize = hm1.size();
        if (lhmSize == 0)
            System.out.println(
                "Given LinkedHashMap is empty");
        else
            System.out.println(
                "Given LinkedHashMap is not empty");
    }
}
Output



Given LinkedHashMap is empty
Given LinkedHashMap is not empty

Time Complexity: O(1)

 

2. Using the isEmpty() method :

  1. Create boolean variable lhmSize.
  2. Store the return value of the isEmpty() method.
  3. If the size of the given LinkedHashMap is 0 then isEmpty() method will return true.
  4. Else method will return false.

Below is the implementation of the above approach:

Java




// Java Program check if LinkedHashMap is empty or not
import java.util.LinkedHashMap;
public class Main {
  
    public static void main(String[] args)
    {
        // create an instance of LinkedHashMap
        LinkedHashMap<Integer, String> hm1
            = new LinkedHashMap<Integer, String>();
  
        //  Using isEmpty() method to make comparison
        System.out.println(
            "Given LinkedHashMap is "
            + (hm1.isEmpty() ? "empty" : "not empty"));
  
        // Add mappings using put method
        hm1.put(3, "Geeks");
        hm1.put(2, "For");
        hm1.put(1, "Geeks");
        boolean lhmSize = hm1.isEmpty();
        if (lhmSize)
            System.out.println(
                "Given LinkedHashMap is empty");
        else
            System.out.println(
                "Given LinkedHashMap is not empty");
    }
}
Output
Given LinkedHashMap is empty
Given LinkedHashMap is not empty

Time Complexity: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :