📘 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
HashMap.isEmpty()
method in Java is used to check if a HashMap
contains no key-value mappings. This guide will cover the method's usage, explain how it works, and provide examples to demonstrate its functionality.Table of Contents
- Introduction
isEmpty
Method Syntax- Examples
- Checking if a HashMap is Empty
- Real-World Use Case: Inventory Check
- Conclusion
Introduction
The isEmpty()
method is a member of the HashMap
class in Java. It allows you to check if the HashMap
is empty, meaning it contains no key-value mappings. This can be useful when you need to determine if the HashMap
has been populated with data or if it is still empty.
isEmpty() Method Syntax
The syntax for the isEmpty
method is as follows:
public boolean isEmpty()
- The method does not take any parameters.
- The method returns a boolean value:
true
if theHashMap
contains no key-value mappings.false
if theHashMap
contains one or more key-value mappings.
Examples
Checking if a HashMap is Empty
The isEmpty
method can be used to check if a HashMap
is empty.
Example
import java.util.HashMap; public class IsEmptyExample { public static void main(String[] args) { // Creating a HashMap with String keys and Integer values HashMap<String, Integer> people = new HashMap<>(); // Checking if the HashMap is empty boolean isEmptyBefore = people.isEmpty(); // Adding entries to the HashMap people.put("Ravi", 25); people.put("Priya", 30); // Checking if the HashMap is empty after adding entries boolean isEmptyAfter = people.isEmpty(); // Printing the results System.out.println("Is HashMap empty before adding entries? " + isEmptyBefore); System.out.println("Is HashMap empty after adding entries? " + isEmptyAfter); } }
Output:
Is HashMap empty before adding entries? true Is HashMap empty after adding entries? false
Real-World Use Case: Inventory Check
In a real-world scenario, you might use the isEmpty
method to check if an inventory is empty before performing operations such as restocking or processing orders.
Example
import java.util.HashMap; public class InventoryCheck { public static void main(String[] args) { // Creating a HashMap with String keys (product names) and Integer values (quantities) HashMap<String, Integer> inventory = new HashMap<>(); // Checking if the inventory is empty if (inventory.isEmpty()) { System.out.println("Inventory is empty. Need to restock."); } else { System.out.println("Inventory has items."); } // Adding products to the inventory inventory.put("Laptops", 10); inventory.put("Smartphones", 15); // Checking if the inventory is empty after adding items if (inventory.isEmpty()) { System.out.println("Inventory is empty. Need to restock."); } else { System.out.println("Inventory has items."); } } }
Output:
Inventory is empty. Need to restock. Inventory has items.
Conclusion
The HashMap.isEmpty()
method in Java provides a way to check if a HashMap
contains no key-value mappings. By understanding how to use this method, you can efficiently determine if a HashMap
has been populated with data. This method is useful in various scenarios, such as checking if an inventory is empty or verifying if a data structure has been initialized with data.
Comments
Post a Comment
Leave Comment