
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Search for an Element in an ArrayList in Java
Java List provides a method indexOf() which can be used to get the location of an element in the list and contains() method to check if object is present or not.
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).
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 showing the usage of indexOf() and contains() methods to search element in a list −
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"); Student missingStudent = new Student(4, "Aman"); System.out.println("Ayan is present at: " + list.indexOf(student)); System.out.println("Aman index: " + list.indexOf(missingStudent)); System.out.println("List contains Ayan: " + list.contains(student)); System.out.println("List contains Aman: " + list.contains(missingStudent)); } } 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 + "]"; } }
This will produce the following result −
Output
List: [[1,Zara], [2,Mahnaz], [3,Ayan]] Ayan is present at: 2 Aman index: -1 List contains Ayan: true List contains Aman: false