Open In App

Find the last element of a Stream in Java

Last Updated : 12 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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

Method 1: Using Stream.reduce()

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:

  • Get the stream of elements in which the first element is to be returned.
  • To get the last element, you can use the reduce() method to ignore the first element, repeatedly, till there is no first element.
    Stream.reduce((first, second) -> second)
  • This reduces the set of elements in a Stream to a single element, which is last.
  • Hence the only single element will be remain in the stream which is the last element.

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));
    }
}

Output
Last Element: Geek_Last


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

  • Get the stream of elements in which the last element is to be returned.
  • To get the last element, you can use the skip() method along with count() method.
    Stream.skip(stream.count()-1)
  • This returns a stream after removing first size()-1 elements, which results in single element stream.
  • This method is followed by findFirst() method, which return the first element which is actually a last element of original stream.

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));
    }
}

Output
Last 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);
    }
}

Output
First element of the reversed stream: 7



Practice Tags :

Similar Reads