🎓 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 contains() method in Java programming and how to use it with an example.
1. Set contains() Method Overview
Definition:
The contains() method of the Java Set interface returns true if the set contains the specified element.
Syntax:
boolean exists = set.contains(Object o);
Parameters:
- Object o: The element whose presence in this set is to be tested.
Key Points:
- The method returns true if this set contains the specified element.
- This method uses the equals method of the elements to check for equality.
- If the set contains the specified element, it returns true, otherwise, it returns false.
- The method throws a ClassCastException if the type of the specified element is incompatible with this set (optional).
- It also throws a NullPointerException if the specified element is null and this set does not support null elements (optional).
2. Set contains() Method Example
import java.util.HashSet;
import java.util.Set;
public class SetContainsExample {
public static void main(String[] args) {
Set<String> fruits = new HashSet<>();
// Adding elements to the set
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Cherry");
// Checking if set contains specific elements
System.out.println("Does the set contain 'Apple'? : " + fruits.contains("Apple")); // true
System.out.println("Does the set contain 'Grape'? : " + fruits.contains("Grape")); // false
// Using contains() with a null value
fruits.add(null); // Assuming the set implementation allows null
System.out.println("Does the set contain null? : " + fruits.contains(null)); // true
}
}
Output:
Does the set contain 'Apple'? : true Does the set contain 'Grape'? : false Does the set contain null? : true
Explanation:
In this example:
1. We create a HashSet of fruits.
2. We add three fruits: "Apple", "Banana", and "Cherry" to the set.
3. We then check for the presence of "Apple" and "Grape" using the contains() method. As expected, "Apple" exists, while "Grape" does not.
4. We then demonstrate how the contains() method works with null values (assuming the set implementation supports null values).
The contains() method is essential to determine the presence of an element in a set without having to traverse the entire set manually.
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