Java EnumMap isEmpty() Method

📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.

🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.

▶️ Subscribe to My YouTube Channel (176K+ subscribers): Java Guides on YouTube

▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube

The EnumMap.isEmpty() method in Java is used to check if the map contains no key-value mappings. This guide will cover the method's usage with examples, and we will also cover a real-world use case to show how EnumMap.isEmpty() can be used effectively.

Table of Contents

  1. Introduction
  2. isEmpty Method Syntax
  3. Examples
    • Basic Usage of isEmpty Method
    • Working with Non-Empty and Empty Maps
  4. Real-World Use Case
    • Example: Checking if an EnumMap of Employee Roles is Empty
  5. Conclusion

Introduction

The EnumMap.isEmpty() method is a member of the EnumMap class in Java. It returns true if the map contains no key-value mappings, and false otherwise.

isEmpty() Method Syntax

The syntax for the isEmpty method is as follows:

public boolean isEmpty() 
  • Returns: true if this map contains no key-value mappings, false otherwise.

Examples

Basic Usage of isEmpty Method

The isEmpty method can be used to check if an EnumMap is empty.

Example

import java.util.EnumMap; public class EnumMapIsEmptyExample { // Define an enum representing months of the year enum Month { JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY, AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER } public static void main(String[] args) { // Create an EnumMap with Month as key and String as value EnumMap<Month, String> holidays = new EnumMap<>(Month.class); // Checking if the EnumMap is empty initially boolean isEmptyInitial = holidays.isEmpty(); System.out.println("Is the holidays map empty initially? " + isEmptyInitial); // Adding entries to the EnumMap holidays.put(Month.JANUARY, "New Year's Day"); holidays.put(Month.DECEMBER, "Christmas Day"); // Checking if the EnumMap is empty after adding entries boolean isEmptyAfterAdding = holidays.isEmpty(); System.out.println("Is the holidays map empty after adding entries? " + isEmptyAfterAdding); } } 

Output:

Is the holidays map empty initially? true Is the holidays map empty after adding entries? false 

Working with Non-Empty and Empty Maps

The isEmpty method returns true for an empty EnumMap and false for a non-empty EnumMap.

Example

import java.util.EnumMap; public class EnumMapEmptyExample { // Define an enum representing types of transportation enum Transportation { CAR, BUS, TRAIN, AIRPLANE, BICYCLE } public static void main(String[] args) { // Create an empty EnumMap with Transportation as key and Integer as value EnumMap<Transportation, Integer> transportMap = new EnumMap<>(Transportation.class); // Checking if the EnumMap is empty initially boolean isEmptyInitial = transportMap.isEmpty(); System.out.println("Is the transport map empty initially? " + isEmptyInitial); // Adding an entry to the EnumMap transportMap.put(Transportation.CAR, 50); // Checking if the EnumMap is empty after adding an entry boolean isEmptyAfterAdding = transportMap.isEmpty(); System.out.println("Is the transport map empty after adding an entry? " + isEmptyAfterAdding); // Clearing the EnumMap transportMap.clear(); // Checking if the EnumMap is empty after clearing it boolean isEmptyAfterClearing = transportMap.isEmpty(); System.out.println("Is the transport map empty after clearing it? " + isEmptyAfterClearing); } } 

Output:

Is the transport map empty initially? true Is the transport map empty after adding an entry? false Is the transport map empty after clearing it? true 

Real-World Use Case

Example: Checking if an EnumMap of Employee Roles is Empty

A common real-world use case for EnumMap.isEmpty() is checking if a map of employee roles and their associated counts is empty.

Example

import java.util.EnumMap; public class EmployeeRolesChecker { // Define an enum representing employee roles enum Role { ENGINEER, MANAGER, HR, SALES, MARKETING } public static void main(String[] args) { // Create an EnumMap to manage counts of each employee role EnumMap<Role, Integer> employeeRoles = new EnumMap<>(Role.class); // Checking if the EnumMap is empty initially boolean isEmptyInitial = employeeRoles.isEmpty(); System.out.println("Is the employee roles map empty initially? " + isEmptyInitial); // Adding roles to the EnumMap employeeRoles.put(Role.ENGINEER, 5); employeeRoles.put(Role.MANAGER, 2); // Checking if the EnumMap is empty after adding roles boolean isEmptyAfterAdding = employeeRoles.isEmpty(); System.out.println("Is the employee roles map empty after adding roles? " + isEmptyAfterAdding); } } 

Output:

Is the employee roles map empty initially? true Is the employee roles map empty after adding roles? false 

In this example, EnumMap.isEmpty() is used to check if the map of employee roles and their counts is empty, making it easy to verify the presence of role assignments.

Conclusion

The EnumMap.isEmpty() method in Java provides a way to check if the map contains no key-value mappings. By understanding how to use this method, you can efficiently manage and verify the presence of entries in collections where the keys are enum constants. This method allows you to determine if an EnumMap is empty, making it a versatile tool for managing data in various scenarios.

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare