
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 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