
- 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 Java list contains an element or not?
List provides a method contains() to check if list contains that element or not. It utilizes equals() method so we need to override the equals() method in the element type.
Syntax
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
o − 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
The following example shows how to check elements to be present in a list using contains() method.
package com.tutorialspoint; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class CollectionsDemo { public static void main(String[] args) { Student s1 = new Student(1, "Zara"); Student s2 = new Student(2, "Mahnaz"); Student s3 = new Student(3, "Ayan"); Student s4 = new Student(4, "Raman"); List<Student> list = new ArrayList<>(Arrays.asList(s2,s1,s3)); System.out.println("List: " + list); System.out.println("Zara is present: " + list.contains(s1)); System.out.println("Raman is present: " + list.contains(s4)); } } class Student { private int rollNo; private String name; public Student(int rollNo, String name) { this.rollNo = rollNo; this.name = name; } public int getRollNo() { return rollNo; } public void setRollNo(int rollNo) { this.rollNo = rollNo; } 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.rollNo == student.getRollNo() && this.name.equals(student.getName()); } @Override public String toString() { return "[" + this.rollNo + "," + this.name + "]"; } }
Output
This will produce the following result −
List: [[2,Mahnaz], [1,Zara], [3,Ayan]] Zara is present: true Raman is present: false
- Related Articles
- Check if a Java ArrayList contains a given item or not
- How to check if a slice contains an element in Golang?
- How to check if an URL is valid or not using Java?
- How to Check if an Array is Empty or Not in Java
- How to check an element exist or not in jQuery?
- How to check if ArrayList contains an item in Java?
- Check if a Stack contains an element in C#
- Python Pandas IntervalIndex - Check if an interval that contains points is empty or not
- How do you check a list contains an item in Java?
- How to check whether a vector contains an NA value or not in R?
- How do you check if an element is present in a list in Java?
- How to check if a file exists or not in Java?
- Check if SortedDictionary contains the specified key or not in C#
- Check if list is sorted or not in Python
- How to check whether a String contains a substring or not?
