
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
How to search in a List of Java object?
A List is a sequence of elements of similar data types, and like an array, its elements are stored at specific indices, which can be accessed and searchable through the index (i.e., starting at index 0).
We can search through each element in the list and retrieve its index (or position) if it is found in the list. We can search in a List in the following ways -
Searching in a List using indexOf() Method
The indexOf() method of the List interface searches and returns the first occurrence of the specified element in the list, or -1 if the list does not contain the specified element. Following is the syntax of the indexOf() method:
int indexOf(Object o)
Here, o is an element that needs to be searched.
Example
In the following example, we use the indexOf() method to search a specified element 30 in the List {10, 20, 30, 40}:
import java.util.ArrayList; import java.util.List; public class searchElement { public static void main(String[] args) { //creating a list List<Integer> list = new ArrayList<>(); //adding element to it list.add(10); list.add(20); list.add(30); list.add(40); System.out.println("The list is: " + list); int ele = 30; System.out.println("The item needs to be searched: " + ele); //using indexOf() method int result = list.indexOf(ele); if(result != -1){ System.out.println("Yes! '" + ele + "' is present in the list."); } } }
The above program produces the following output:
The list is: [10, 20, 30, 40] The item needs to be searched: 30 Yes! '30' is present in the list.
Using lastIndexOf() Method
The lastIndexOf() method of the List interface searches and returns an index of the last occurrence of the specified element in this list, or -1 if this list does not contain the specified element. Following is the syntax of the lastIndexOf() method:
int lastIndexOf(Object o)
Here, o is an element that needs to be searched.
Example
The following program uses the lastIndexOf() method to search and return the index of the last occurrence of the specified element in the list {1, 2, 3, 4, 3, 2, 1}:
import java.util.ArrayList; import java.util.List; public class searchElement { public static void main(String[] args) { //creating a list List<Integer> list = new ArrayList<>(); //adding element to it list.add(1); list.add(2); list.add(3); list.add(4); list.add(3); list.add(2); list.add(1); System.out.println("The list is: " + list); int ele = 2; System.out.println("The item needs to searched: " + ele); //using lastIndexOf() method int result = list.lastIndexOf(ele); if(result != -1){ System.out.println("Yes!, the element " + ele + " is present in the List."); } } }
Following is the output of the above program:
The list is: [1, 2, 3, 4, 3, 2, 1] The item needs to searched: 2 Yes!, the element 2 is present in the List
Using the Comparison Approach
This is another way of searching in a List. We will iterate through the list and compare each element with the given element (using == operator). If the element is found equal to any of the list elements, the index of that element will be displayed.
Example
In this example, we loop through the list to compare the given element with each item in the list. If they are equal, the index (position) of the element in the list will be displayed:
import java.util.ArrayList; import java.util.List; public class searchElement { public static void main(String[] args) { //creating a list List<String> vowels = new ArrayList<>(); //adding element to it vowels.add("A"); vowels.add("E"); vowels.add("I"); vowels.add("O"); vowels.add("U"); System.out.println("The list is: " + vowels); String ele = "O"; System.out.println("The item needs to searched: " + ele); for(int i = 0; i<vowels.size(); i++){ if(vowels.get(i) == ele){ System.out.println("The element '" + ele +"' is found at index " + i); break; } } } }
Below is the output of the above program:
The list is: [A, E, I, O, U] The item needs to searched: O The element 'O' is found at index 3