🎓 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 (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
In this guide, you will learn about the Set isEmpty() method in Java programming and how to use it with an example.
1. Set isEmpty() Method Overview
Definition:
The isEmpty() method of the Java Set interface returns true if the set contains no elements.
Syntax:
boolean isSetEmpty = set.isEmpty();
Parameters:
None.
Key Points:
- The method returns true if this set contains no elements.
- It's a more readable alternative to checking if set.size() == 0.
- Useful in scenarios where you want to take some action only if the set is empty.
2. Set isEmpty() Method Example
import java.util.HashSet;
import java.util.Set;
public class SetIsEmptyExample {
public static void main(String[] args) {
Set<String> fruits = new HashSet<>();
// Checking if the set is empty initially
System.out.println("Is the set empty? : " + fruits.isEmpty()); // true
// Adding an element and checking again
fruits.add("Apple");
System.out.println("Is the set empty after adding an element? : " + fruits.isEmpty()); // false
// Clearing the set and checking again
fruits.clear();
System.out.println("Is the set empty after clearing? : " + fruits.isEmpty()); // true
}
}
Output:
Is the set empty? : true Is the set empty after adding an element? : false Is the set empty after clearing? : true
Explanation:
In this example:
1. We create a HashSet of fruits.
2. Initially, the set is empty, and isEmpty() returns true.
3. After adding "Apple" to the set, isEmpty() returns false as the set now contains an element.
4. Finally, we clear the set using the clear() method. isEmpty() then again returns true since the set is empty.
The isEmpty() method is a straightforward way to check for the absence of elements in a set without having to inspect its size or content directly.
Related Java Set interface methods
Java Set add() exampleJava Set contains() example
Java Set isEmpty() example
Java Set remove() example
Java Set size() example
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment