
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to check if ArrayList contains an item in Java?
You can utilize contains() method of the List interface to check if an object is present 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)).
Parameter
c − Element whose presence in this list is to be tested.
Returns
True if this list contains the specified element.
Throws
ClassCastException − If the type of the specified element is incompatible with this list (optional).
NullPointerException − If the specified element is null and this list does not permit null elements (optional).
Example
Following is the example showing the usage of contains() methods −
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.
- Related Articles
- Check if a Java ArrayList contains a given item or not
- How do you check a list contains an item in Java?
- How to add an item to an ArrayList in C#?
- How to add an item to an ArrayList in Kotlin?
- How to insert an item in ArrayList in C#?
- How to check if Java list contains an element or not?
- How to remove an item from an ArrayList in C#?
- How to remove an item from an ArrayList in Kotlin?
- How to check if an item exists in a C# array?
- Java Program to Check if An Array Contains a Given Value
- Java Program to Check if An Array Contains the Given Value
- How to check if an item exists in a C# list collection?
- How to check if array contains three consecutive dates in java?
- How to check if an array contains integer values in JavaScript ?
- How to check if a slice contains an element in Golang?

Advertisements