How to find the last occurrence of an element in a Java List?



In this article, we will learn how to find the last occurrence of an element in a Java List. It is a collection in which we can store and access elements in serial order.

We can use the following ways to find the last occurrence of an element in a Java List:

Using a For Loop

We would use a for loop to iterate through the list in reverse order and check if the desiredelement is equal to any of the other elements in the list. If it is equal, we will return the index of that element.

Example

In the below example, we will create a list of integers and then use a for loop to find the last occurrence of an element in that list.

import java.util.ArrayList;
import java.util.List;  
public class LastOccurrenceInList {
   public static void main(String[] args) {
      List<Integer> list = new ArrayList<>();
      
      list.add(1);
      list.add(2);
      list.add(3);
      list.add(4);
      list.add(5);
      list.add(3); 
      
      int elementToFind = 3;
      int lastIndex = -1;

      for (int i = list.size() - 1; i >= 0; i--) {
         if (list.get(i).equals(elementToFind)) {
            lastIndex = i;
            break;
         }
      }

      System.out.println("Last occurrence of " + elementToFind + " is at index: " + lastIndex);
   }
}

Output

Following is the output of the above code:

Last occurrence of 3 is at index: 5

LastIndexOf() Method

The lastIndexOf() is a built-in method of the List interface in Java. It is used to find the last occurrence of a particular element in a list.

Example

In the below example, we will create a list of integers and then use the lastIndexOf() method to find the last occurrence of an element in that list without using any loop.

import java.util.ArrayList;
import java.util.List;
public class LastOccurrenceInList {
   public static void main(String[] args) {
      List<Integer> list = new ArrayList<>();
      
      list.add(1);
      list.add(2);
      list.add(3);
      list.add(4);
      list.add(5);
      list.add(3); 
      
      int elementToFind = 3;
      int lastIndex = list.lastIndexOf(elementToFind);

      System.out.println("Last occurrence of " + elementToFind + " is at index: " + lastIndex);
   }
}

Output

Following is the output of the above code:

Last occurrence of 3 is at index: 5

Using Stream API

The Stream API was introduced in Java 8 provides a functional approach to working with collections. We can use the filter() method to filter the elements in the list and then use the reduce() method to find the last occurrence of an element.

Example

Create a list of integers and then use the Stream API to find the last occurrence of an element in that list.

import java.util.ArrayList;
import java.util.List;
import java.util.OptionalInt;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class LastOccurrenceInList {
   public static void main(String[] args) {
      List<Integer> list = new ArrayList<>();
      
      list.add(1);
      list.add(2);
      list.add(3);
      list.add(4);
      list.add(5);
      list.add(3); 
      
      int elementToFind = 3;
      
      OptionalInt lastIndex = IntStream.range(0, list.size())
         .filter(i -> list.get(i).equals(elementToFind))
         .reduce((first, second) -> second);

      System.out.println("Last occurrence of " + elementToFind + " is at index: " + lastIndex.orElse(-1));
   }
}

Output

Following is the output of the above code:

Last occurrence of 3 is at index: 5
Aishwarya Naglot
Aishwarya Naglot

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

Updated on: 2025-06-10T15:43:14+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements