How do you get the index of an element in a list in Java?



List is a collection in Java that allows us to store elements. In this article, we will learn how to get the index of an element in a List in Java.

We have various ways to get the index of an element in a List in Java. They are -

Using the indexOf() method

The indexOf() method returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element. In other words, this function returns the lowest index i such that (o==null ? get(i)==null: o.equals(get(i))), or -1 if there is no such index.

Example

We will create a List of string type and add some values to it. Then we will use the indexOf() method to find the index of a specific value in the List.

import java.util.ArrayList;
import java.util.List;
public class GetIndexOfElement {
   public static void main(String[] args) {
      List<String> list = new ArrayList<>();
      
      list.add("Apple");
      list.add("Banana");
      list.add("Cherry");
      list.add("Date");
      
      String elementToFind = "Cherry";
      
      int index = list.indexOf(elementToFind);
      
      if (index != -1) {
         System.out.println("The element '" + elementToFind + "' is found at index: " + index);
      } else {
         System.out.println("The element '" + elementToFind + "' is not found in the List.");
      }
   }
}

Output

Following is the output of the above code:

The element 'Cherry' is found at index: 2

Using a for loop

We will loop through the list and check each element if it matches the element we are looking for. If it does, we will return the index of that element.

Example

Following is the Java program to get the index of an element in a List using a for loop:

import java.util.ArrayList;
import java.util.List;
public class GetIndexOfElement {
   public static void main(String[] args) {
      List<String> list = new ArrayList<>();
      
      list.add("Apple");
      list.add("Banana");
      list.add("Cherry");
      list.add("Date");
      
      String elementToFind = "Banana";
      
      int index = -1;
      
      for (int i = 0; i < list.size(); i++) {
         if (list.get(i).equals(elementToFind)) {
            index = i;
            break;
         }
      }
      
      if (index != -1) {
         System.out.println("The element '" + elementToFind + "' is found at index: " + index);
      } else {
         System.out.println("The element '" + elementToFind + "' is not found in the List.");
      }
   }
}

Output

Following is the output of the above code:

The element 'Banana' is found at index: 1

Using Stream API

We can also use the Stream API to find the index of an element in a List. We will use methods like Stream.filter() and Stream.findFirst() to achieve this.

Example

Below is an example of getting the index of an element in a List using the Stream API:

In this example, we will create a List of string type and add some values to it. Then we will use the Stream API to find the index of a specific value in the List.

import java.util.ArrayList;
import java.util.List;
import java.util.stream.IntStream;
public class GetIndexOfElement {
   public static void main(String[] args) {
      List<String> list = new ArrayList<>();
      
      list.add("Apple");
      list.add("Banana");
      list.add("Cherry");
      list.add("Date");
      
      String elementToFind = "Date";
      
      int index = IntStream.range(0, list.size())
                           .filter(i -> list.get(i).equals(elementToFind))
                           .findFirst()
                           .orElse(-1);
      
      if (index != -1) {
         System.out.println("The element '" + elementToFind + "' is found at index: " + index);
      } else {
         System.out.println("The element '" + elementToFind + "' is not found in the List.");
      }
   }
}

Output

Following is the output of the above code:

The element 'Date' is found at index: 3
Aishwarya Naglot
Aishwarya Naglot

Writing clean code… when the bugs aren’t looking.

Updated on: 2025-06-05T12:36:24+05:30

21K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements