Find an Element in a List with Java



In Java, a List is an interface that extends the Collection interface and represents a sequence of elements.

Since the List is an interface, we can not directly instantiate it. We need to use a class that implements the List interface, such as ArrayList.

The List provides various methods that help to check or find the element in it. We will discuss those methods in the coming section with suitable examples.

Below is a list of various ways to find an element in the Java List:

Finding the List Element using the get() Method

The get() method of the List interface is used to find (retrieve) an element in the List. This method accepts an index as a parameter and returns the element at the specified position in the List.

Following is the syntax of the get() method:

get(index)

Here, an "index" is the element's position in a List.

Example

In the following example, we use the get() method to retrieve an element at the specified index 2 in this List {10, 20, 30, 40, 50}:

import java.util.ArrayList;
import java.util.List;


public class findElement {
   public static void main(String[] args) {
   
      //creating a list
      List<Integer> list = new ArrayList<>();
      
      //adding elements
      list.add(10);
      list.add(20);
      list.add(30);
      list.add(40);
      list.add(50);
      
      System.out.println("The list elements are: " + list);
      int index = 2;
      System.out.println("An index to which element will find: " + index);
      
      //using get() method
      System.err.println("The list element at index " + index + " is: " + list.get(index));
   }
}

The above program produces the following output:

The list elements are: [10, 20, 30, 40, 50]
An index to which element will find: 2
The list element at index 2 is: 30

Using an indexOf() Method

You can also find an element in a List using the indexOf() method of the List interface. This method returns the index of the first occurrence of the specified element in this list, or -1 if it does not.

Following is the syntax of the indexOf() method:

indexOf(Object o)

Here, o is an element that will be searched in the List.

Example

In the example below, an indexOf() method is used to find the index of the specified element "Banana" in this List {"Apple", "Banana", "Orange", "Papaya"}, if the index value is not equal to -1, it means the element is found:

import java.util.ArrayList;
import java.util.List;
public class findElement {
   public static void main(String[] args) {
   
      //creating a list
      List<String> list = new ArrayList<>();
      list.add("Apple");
      list.add("Banana");
      list.add("Orange");
      list.add("Papaya");
      
      System.out.println("The list elements are: " + list);
      String fruit = "Banana";
      System.out.println("The element to search: " + fruit);
	  
      //using indexOf() method
      if(list.indexOf(fruit) != -1) {
         System.out.println(fruit + " is found in the list.");
      }
      else{
         System.out.println(fruit + " does not found in the list");
      }
   }
}

Following is the output of the above program:

The list elements are: [Apple, Banana, Orange, Papaya]
The element to search: Banana
Banana is found in the list.

Using contains() Method

Using the contains() method is another way to find an element in a List. This method returns true if the List contains at least one element that is equal to the specified element; or else false. Following is the syntax of the contains() method:

contains(Object o)

Here, o is an element whose presence in this list is to be checked.

Example

The following example uses the contains() method to check whether the list {1, 2, 3, 4} contains the specified element "4" or not:

import java.util.ArrayList;
import java.util.List;
public class findElement {
   public static void main(String[] args) {
   
      //creating a list
      List<Integer> list = new ArrayList<>();
      list.add(1);
      list.add(2);
      list.add(3);
      list.add(4);
      
      System.out.println("The list elements are: " + list);
      int num = 4;
      System.out.println("The element to search: " + num);
      
      //using contains() method
      if(list.contains(num)) {
         System.out.println(num + " is found in the list.");
      }
      else{
         System.out.println(num + " does not found in the list");
      }
   }
}

Below is the output of the above program:

The list elements are: [1, 2, 3, 4]
The element to search: 4
4 is found in the list.
Updated on: 2025-05-16T19:20:46+05:30

23K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements