
- 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 do you check a list contains an item in Java?
Elements can be checked from a list using indexOf() or contains() methods.
Syntax - indexOf() method
int indexOf(Object o)
Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element. More formally, returns the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))), or -1 if there is no such index.
Parameters
o − Element to search for.
Returns
The index of the first occurrence of the specified element in this list, or -1 if this list does not contain the 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).
Syntax - 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
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
Following is the example of finding elements from a list using various methods −
package com.tutorialspoint; import java.util.ArrayList; import java.util.List; public class CollectionsDemo { public static void main(String[] args) { List<Student> 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"); System.out.println("Ayan is present: " + list.contains(student)); int index = list.indexOf(student); System.out.println("Ayan is present at: " + index); } } 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 −
List: [[1,Zara], [2,Mahnaz], [3,Ayan]] Ayan is present: true Ayan is present at: 2
- Related Articles
- How to check if ArrayList contains an item in Java?
- How do you check if an element is present in a list in Java?
- How do I insert an item between two items in a list in Java?
- How do you create an empty list in Java?
- How do you add an element to a list in Java?
- How to check if Java list contains an element or not?
- Check if a Java ArrayList contains a given item or not
- How do you copy a list in Java?
- How do you create a list in Java?
- How to check if an item exists in a C# list collection?
- How do you make a list iterator in Java?
- How do you get the index of an element in a list in Java?
- How do you create a list with values in Java?
- How do you create a list from a set in Java?
- How do you turn a list into a Set in Java?
