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:
Methods to Find the Last Element of a Stream in JavaInput: Stream={“Geek_First”, “Geek_2”, “Geek_3”, “Geek_4”, “Geek_Last”}
Output: Geek_LastInput: Stream={1, 2, 3, 4, 5, 6, 7}
Output: 7
There are many methods to find the last elements in a 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:
Stream.reduce((first, second) -> second)
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));
}
}
Last Element: Geek_LastMethod 2: Using Stream skip()
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:
Stream.skip(stream.count()-1)
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));
}
}
Last Element: Geek_LastMethod 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:
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);
}
}
First element of the reversed stream: 7
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4