🎓 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 size() method in Java programming and how to use it with an example.
1. Set size() Method Overview
Definition:
The size() method of the Java Set interface returns the number of elements in the set.
Syntax:
int numberOfElements = set.size();
Parameters:
- None.
Key Points:
- The method returns the count of elements present in the set.
- If the Set is empty, the method returns 0.
- The size returned accounts for the unique elements in the Set since a set does not allow duplicate elements.
2. Set size() Method Example
import java.util.HashSet;
import java.util.Set;
public class SetSizeExample {
public static void main(String[] args) {
Set<String> fruits = new HashSet<>();
// Check the size of an empty set
System.out.println("Size of empty set: " + fruits.size()); // 0
// Adding some elements to the set
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Cherry");
// Check the size after adding elements
System.out.println("Size after adding three fruits: " + fruits.size()); // 3
// Adding duplicate elements
fruits.add("Apple");
fruits.add("Banana");
// Check the size after adding duplicate elements
System.out.println("Size after adding duplicate fruits: " + fruits.size()); // 3
}
}
Output:
Size of empty set: 0 Size after adding three fruits: 3 Size after adding duplicate fruits: 3
Explanation:
In this example:
1. We create a HashSet of fruits and initially check its size, which is 0 since it's empty.
2. We add three elements: "Apple", "Banana", and "Cherry" to the set and then check its size, which returns 3.
3. Next, we add two duplicate elements: "Apple" and "Banana". Since a set does not allow duplicate elements, these additions do not increase the size of the set. Thus, checking the size again returns 3.
The size() method is handy when you want to quickly assess the number of unique elements in a set. It provides a clear indication of the set's capacity without the need for iterating over its elements.
Related Java Set interface methods
Java Set add() example
Java 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