Find the last element of a Stream in Java
Last Updated :
12 Jul, 2025
Given a stream containing some elements, the task is to get the last element of the Stream in Java.
Example:
Input: Stream={“Geek_First”, “Geek_2”, “Geek_3”, “Geek_4”, “Geek_Last”}
Output: Geek_Last
Input: Stream={1, 2, 3, 4, 5, 6, 7}
Output: 7
Methods to Find the Last Element of a Stream in Java
There are many methods to find the last elements in a Stream:
- Using stream.reduce()
- Using stream.skip()
- By finding the first element of a reversed Stream
The reduce method works on two elements in the stream and returns the element as per the required condition. Therefore this method can be used to reduce the stream so that it contains only the last element.
Approach:
Below is the implementation of the above approach.
Example:
Java
// Java program to find last
// element of a Stream in Java using stream.reduce() method
import java.util.*;
import java.util.stream.*;
public class GFG {
// Function to find the
// last_elements in a Stream
public static <T> T
lastElementInStream(Stream<T> stream)
{
T last_element
= stream
// reduce() method reduces the Stream
// to a single element, which is last.
.reduce((first, second) -> second)
// if stream is empty
// null is returned
.orElse(null);
return last_element;
}
// Driver code
public static void main(String[] args)
{
Stream<String> stream
= Stream.of("Geek_First", "Geek_2",
"Geek_3", "Geek_4",
"Geek_Last");
// Print the last element of a Stream
System.out.println(
"Last Element: "
+ lastElementInStream(stream));
}
}
OutputLast Element: Geek_Last
The skip() method returns a stream after removing first N elements. Therefore this method can be used to skip the elements except the last one.
Approach:
Below is the implementation of the above approach.
Example:
Java
// Java program to find last
// element of a Stream using stream.skip() method
import java.util.*;
import java.util.stream.*;
public class GFG {
// Function to find the
// last_elements in a Stream
public static <T> T
lastElementInStream(Stream<T> stream, int N)
{
T last_element
= stream
// This returns a stream after
// removing first N-1 elements
// resultant stream will contain
// only single element
.skip(N - 1)
// findFirst() method return
// the first element of
// newly generated stream
.findFirst()
// if stream is empty
// null is returned
.orElse(null);
return last_element;
}
// Driver code
public static void main(String[] args)
{
Stream<String> stream
= Stream.of("Geek_First", "Geek_2",
"Geek_3", "Geek_4",
"Geek_Last");
int N = 5;
// Print the last element of a Stream
System.out.println(
"Last Element: "
+ lastElementInStream(stream, N));
}
}
OutputLast Element: Geek_Last
Method 3: By finding the first element of the reversed stream
To find the last element of a stream using the approach of reversing the stream, first reverse the order of elements using sorted(Collections.reverseOrder())
, then use findFirst()
to obtain the first element of this reversed stream. This first element of the reversed stream will be the last element of the original stream.
Approach:
- Create a stream of the elements.
- Reverse the stream using the sorted(Collections.reverseOrder()).
- Use findFirst() to the obtain the first element of the reversed stream.
Below is the implementation of the above approach.
Example:
Java
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
// Get the first element of the reversed stream
Integer firstElementOfReversedStream = numbers.stream()
.sorted(Collections.reverseOrder())
.findFirst()
.orElse(null);
// Handle the case where the stream might be empty
System.out.println("First element of the reversed stream: " + firstElementOfReversedStream);
}
}
OutputFirst element of the reversed stream: 7
Similar Reads
Find the first element of a Stream in Java Given a stream containing some elements, the task is to get the first element of the Stream in Java. Example: Input: Stream = {"Geek_First", "Geek_2", "Geek_3", "Geek_4", "Geek_Last"} Output: Geek_First Input: Stream = {1, 2, 3, 4, 5, 6, 7} Output: 1 There are many methods to the find first elements
3 min read
Find the first element of a Stream in Java Given a stream containing some elements, the task is to get the first element of the Stream in Java. Example: Input: Stream = {"Geek_First", "Geek_2", "Geek_3", "Geek_4", "Geek_Last"} Output: Geek_First Input: Stream = {1, 2, 3, 4, 5, 6, 7} Output: 1 There are many methods to the find first elements
3 min read
Java Program to Find the K'th largest element in a stream Given an infinite stream of integers, find the k'th largest element at any point of time.Example: Input: stream[] = {10, 20, 11, 70, 50, 40, 100, 5, ...} k = 3 Output: {_, _, 10, 11, 20, 40, 50, 50, ...} Extra space allowed is O(k). Recommended: Please solve it on "PRACTICE" first, before moving on
4 min read
How to find duplicate elements in a Stream in Java Given a stream containing some elements, the task is to find the duplicate elements in this stream in Java. Examples: Input: Stream = {5, 13, 4, 21, 13, 27, 2, 59, 59, 34} Output: [59, 13] Explanation: The only duplicate elements in the given stream are 59 and 13. Input: Stream = {5, 13, 4, 21, 27,
5 min read
How to find duplicate elements in a Stream in Java Given a stream containing some elements, the task is to find the duplicate elements in this stream in Java. Examples: Input: Stream = {5, 13, 4, 21, 13, 27, 2, 59, 59, 34} Output: [59, 13] Explanation: The only duplicate elements in the given stream are 59 and 13. Input: Stream = {5, 13, 4, 21, 27,
5 min read
Remove First and Last Elements from LinkedList in Java The Java.util.LinkedList.removeFirst() method is used to remove the first element from the LinkedList. The Java.util.LinkedList.removeLast() method is used to remove the last element from the LinkedList. Both the methods also returns the element after removing it. 1. removeFirst() Syntax: LinkedList
2 min read