Find the Last Element of a Stream in Java


In Java, streams are a capable feature introduced in Java 8 that permits for productive preparation of collections and arrangements of components. Streams give a wide run of operations to perform computations on the components, including filtering, mapping, and diminishment. One common assignment when working with streams is finding the last element. In this article, we'll investigate distinctive approaches to discover the last element of a stream in Java.

Syntax

To work with streams in Java, the syntax includes making a stream from a data source, applying intermediate operations to convert the stream, and ending with a terminal operation. The common sentence structure for finding the final component of a stream is as takes after −

Optional<T> lastElement = stream.reduce((first, second) -> second);

Explanation of Syntax

  • Optional<T> − The result of the last element is wrapped in an Optional object to handle the possibility of an empty stream.

  • stream − The stream on which the operation is performed.

  • reduce() − A terminal operation that reduces the stream to a single value.

  • (first, second) -> second − The lambda expression used as the binary operator for the reduction operation. It returns the second element of the pair, effectively keeping only the last element.

Approach 1: Using the reduce() Method

Algorithm

  • Create a stream from the collection or data source.

  • Apply the reduce() terminal operation with a binary operator that returns the second element of the pair.

  • Retrieve the result as an Optional object.

  • Check if the stream was not empty and get the last element if present.

Example

import java.util.Optional;
import java.util.stream.Stream;

public class LastElementFinder {
   public static <T> Optional<T> findLastElement(Stream<T> stream) {
      return stream.reduce((first, second) -> second);
   }

   public static void main(String[] args) {
      Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5);
      Optional<Integer> lastElement = findLastElement(stream);
      lastElement.ifPresent(System.out::println);
   }
}

Output

5

Explanation

We characterize a findLastElement() strategy that takes a Stream as input and returns an Optional representing the last element.

Within the main() strategy, we make a stream of integrability and call the findLastElement() strategy to recover the last element.

At long last, we utilize the ifPresent() strategy to print the last element in case it exists.

Approach 2: Converting the Stream to an Array

Algorithm

  • Create a stream from the collection or data source.

  • Convert the stream to an array using the toArray() method.

  • Retrieve the last element from the array.

Example

import java.util.Arrays;
import java.util.Optional;
import java.util.stream.Stream;

public class LastElementFinder {
   public static <T> Optional<T> findLastElement(Stream<T> stream) {
      Object[] array = stream.toArray();
      if (array.length > 0) {
         return Optional.of((T) array[array.length - 1]);
      }
      return Optional.empty();
   }

   public static void main(String[] args) {
      Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5, 6, 7, 8);
      Optional<Integer> lastElement = findLastElement(stream);
      lastElement.ifPresent(System.out::println);
   }
}

Output

Note: LastElementFinder.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
8

Explanation

We characterize a findLastElement() strategy that takes a Stream as input and returns an Optional representing the last element.

Inside the strategy, we change over the stream to an array utilizing the toArray() strategy.

In the event that the array has components, we recover the last element by getting to array[array.length - 1].

Finally, we return the last element wrapped in an Optional object.

Approach 3: Using the collect() Method with Collectors.reducing()

Algorithm

  • Create a stream from the collection or data source.

  • Use the collect() terminal operation with Collectors.reducing() to reduce the stream to a single element.

  • Retrieve the result as an Optional object.

Example

import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class LastElementFinder {
   public static <T> Optional<T> findLastElement(Stream<T> stream) {
      return stream.collect(Collectors.reducing((first, second) -> second));
   }

   public static void main(String[] args) {
      Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5, 6);
      Optional<Integer> lastElement = findLastElement(stream);
      lastElement.ifPresent(System.out::println);
   }
}

Output

6

Explanation

We characterize a findLastElement() strategy that takes a Stream as input and returns an Optional representing the last element.

Inside the method, we use the collect() terminal operation with Collectors.reducing() and provide the same binary operator as in previous approaches.

The Collectors.reducing() method reduces the stream to a single element using the provided binary operator.

Finally, we return the result as an Optional object.

Approach 4: Using the Stream.iterate() Method

Algorithm

  • Create a stream using Stream.iterate() and provide the first element and a lambda expression that generates the next element.

  • Use the reduce() terminal operation with the same binary operator as in previous approaches.

  • Retrieve the result as an Optional object.

Example

import java.util.Optional;
import java.util.stream.Stream;

public class LastElementFinder {
   public static <T> Optional<T> findLastElement(Stream<T> stream) {
      return stream.reduce((first, second) -> second);
   }

   public static void main(String[] args) {
      Stream<Integer> stream = Stream.iterate(1, i -> i + 1).limit(5);
      Optional<Integer> lastElement = findLastElement(stream);
      lastElement.ifPresent(System.out::println);
   }
}

Output

5

Explanation

We characterize a findLastElement() strategy that takes a Stream as input and returns an Optional speaking to the last element.

Within the fundamental() strategy, we make a stream using Stream.iterate() and indicate the primary component and a lambda expression to produce the other component.

We utilize the limit() strategy to limit the stream to a certain number of components.

At long last, we call the findLastElement() strategy to recover the last element and print it in the event that displays.

Conclusion

In this article, we investigated diverse approaches to discover the final component of a stream in Java. We examined utilizing the reduce() strategy, changing over the stream to a cluster, utilizing the collect() strategy with Collectors.reducing(), and utilizing the Stream.iterate() strategy. Each approach gives an arrangement to this common assignment, and the choice of approach depends on the particular prerequisites and imperatives of your application. By leveraging the control of streams in Java, you'll productively handle collections and groupings of components, making your code more brief and expressive.

Updated on: 31-Jul-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements