🎓 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
ArrayList.contains() method in Java is used to check if a specified element is present in the ArrayList. This guide will cover the method's usage, explain how it works, and provide examples to demonstrate its functionality.Table of Contents
- Introduction
containsMethod Syntax- Examples
- Checking for the Presence of an Element
- Handling
nullValues
- Real-World Use Case
- Conclusion
Introduction
The contains() method is part of the ArrayList class in Java, and it is used to determine whether a specified element exists within the list. This method is useful when you need to verify the presence of an element before performing certain operations.
contains Method Syntax
The syntax for the contains method is as follows:
public boolean contains(Object o)
- o: The element whose presence in the
ArrayListis to be tested.
The method returns true if the ArrayList contains the specified element, and false otherwise.
Examples
Checking for the Presence of an Element
The contains method can be used to check if an ArrayList contains a specific element.
Example
import java.util.ArrayList;
import java.util.List;
public class ContainsExample {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Orange");
// Check if the list contains "Banana"
boolean containsBanana = list.contains("Banana");
System.out.println("Contains Banana: " + containsBanana);
}
}
Output:
Contains Banana: true
Handling null Values
The contains method can also be used to check for null values in the ArrayList.
Example
import java.util.ArrayList;
import java.util.List;
public class ContainsNullExample {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("Apple");
list.add(null);
list.add("Orange");
// Check if the list contains null
boolean containsNull = list.contains(null);
System.out.println("Contains null: " + containsNull);
}
}
Output:
Contains null: true
Real-World Use Case
User Authentication
In a user authentication system, you may need to check if a username exists in a list of registered users before allowing a login attempt.
Example
import java.util.ArrayList;
import java.util.List;
public class UserAuthentication {
public static void main(String[] args) {
List<String> registeredUsers = new ArrayList<>();
registeredUsers.add("john_doe");
registeredUsers.add("jane_smith");
registeredUsers.add("alice_jones");
// Check if the username exists
String usernameToCheck = "jane_smith";
boolean isRegistered = registeredUsers.contains(usernameToCheck);
if (isRegistered) {
System.out.println("Username '" + usernameToCheck + "' is registered.");
} else {
System.out.println("Username '" + usernameToCheck + "' is not registered.");
}
}
}
Output:
Username 'jane_smith' is registered.
Conclusion
The ArrayList.contains() method in Java provides a simple way to check if an element is present in the list. By understanding how to use this method, you can efficiently verify the presence of elements in your lists in Java applications. Whether you are checking for specific values or handling null elements, the contains() method offers a straightforward and effective solution.
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