How to check if ArrayList contains a certain element in Java?
You can use the contains() method of the List interface to check whether an object exists in the list.
contains() method
boolean contains(Object o)
Returns true if this list contains the specified element. More formally, returns true if and only if this list contains at least one element e such that (o==null ? e==null : o.equals(e)).
Parameters
c - The element whose presence in this list is to be tested.
Return value
Returns true if this list contains the specified element.
Throws
ClassCastException - if the specified element's type is incompatible with this list (optional).
NullPointerException - if the specified element is null and this list does not allow null elements (optional).
Example
The following is an example of using the contains() method:
package com.tutorialspoint; import java.util.ArrayList; import java.util.List; public class CollectionsDemo { public static void main(String[] args) { List list = new ArrayList<>(); list.add(new Student(1, "Zara")); list.add(new Student(2, "Mahnaz")); list.add(new Student(3, "Ayan")); System.out.println("List: " + list); Student student = new Student(3, "Ayan"); if(list.contains(student)) { System.out.println("Ayan is present."); } } } class Student { private int id; private String name; public Student(int id, String name) { this.id = id; this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public boolean equals(Object obj) { if(!(obj instanceof Student)) { return false; } Student student = (Student)obj; return this.id == student.getId() && this.name.equals(student.getName()); } @Override public String toString() { return "[" + this.id + "," + this.name + "]"; } }
Output
This will produce the following result-
Note: com/tutorialspoint/CollectionsDemo.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. List: [[1,Zara], [2,Mahnaz], [3,Ayan]] Ayan is present.
The above is the detailed content of How to check if ArrayList contains a certain element in Java?. For more information, please follow other related articles on the PHP Chinese website!

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 English version
Recommended: Win version, supports code prompts!

Notepad++7.3.1
Easy-to-use and free code editor

Atom editor mac version download
The most popular open source editor