Find the first element of a Stream in Java
Finding the first element of a stream in Java involves leveraging the Stream API provided by the language. Streams provide a way to process collections of objects in a functional style, allowing operations to be chained together easily.
Examples:
Example 1:
Input: Stream of Strings containing {"apple", "banana", "orange"}
Output: "apple"
Explanation:
Given a stream of strings representing fruits, the findFirst() method returns the first element of the stream, which is "apple".
Example 2:
Input: Stream of Integers containing {10, 20, 30, 40, 50}
Output: 10
Explanation:
In this example, the stream consists of integers. By applying the findFirst() method, the first element of the stream, which is 10, is retrieved.
Example 3:
Input: Empty Stream
Output: "Stream is empty!"
Explanation:
When the stream is empty, invoking the findFirst() method results in an empty optional. Consequently, the program prints a message indicating that the stream is empty.
Approach 1: Using findFirst()
ALGORITHM:
Step 1: Define a Stream of elements using Stream.of() or other stream sources.
Step 2: Create a method findFirstElement that takes a Stream
Step 3: Use the findFirst() method on the stream to get the first element as an Optional
Step 4: Check if the Optional contains a value using isPresent().
Step 5: Print the first element if present, or indicate that the stream is empty.
Implementation:
The implementation of the above steps given below
FileName: StreamExample1.java
import java.util.Arrays; import java.util.Optional; import java.util.stream.Stream; public class StreamExample1 { public static void main(String[] args) { // Example 1: Stream of Strings StreamstringStream = Stream.of("apple", "banana", "orange"); findFirstElement(stringStream); // Example 2: Stream of Integers Stream integerStream = Stream.of(10, 20, 30, 40, 50); findFirstElement(integerStream); // Example 3: Empty Stream Stream
Output:
First element of the stream: apple First element of the stream: 10 Stream is empty!
Method 2: Using findAny()
ALGORITHM:
Step 1: Define a stream of elements using Stream.of() or other stream sources, such as a stream of Strings or Integers.
Step 2: Create a method findAnyElement that takes a Stream
Step 3: Inside the findAnyElement method, use the findAny() method on the input stream to retrieve an Optional
Step 4: Check if the Optional returned by findAny() contains a value using the isPresent() method.
Step 5: If an element is present, retrieve it using get() and print it. If the stream is empty, indicate that the stream is empty.
Implementation:
The implementation of the above steps given below
FileName: StreamExample2.java
import java.util.Optional; import java.util.stream.Stream; public class StreamExample2 { public static void main(String[] args) { // Example 1: Stream of Strings StreamstringStream = Stream.of("apple", "banana", "orange"); findAnyElement(stringStream); // Example 2: Stream of Integers Stream integerStream = Stream.of(10, 20, 30, 40, 50); findAnyElement(integerStream); // Example 3: Empty Stream Stream
Output:
Any element of the stream: apple Any element of the stream: 10 Stream is empty!
Method 3: using reduce()
ALGORITHM:
Step 1: Define a stream of elements using Stream.of() or other stream sources.
Step 2: Create a method findFirstElement that takes a Stream
Step 3: Inside the findFirstElement method, use the reduce() method on the input stream to retrieve an Optional
Step 3.1: The reduce() method takes a binary operator that takes two elements and returns the first element, effectively returning the first element of the stream.
Step 4: Check if the Optional returned by reduce() contains a value using the isPresent() method.
Step 5: If an element is present, retrieve it using get() and print it. If the stream is empty, indicate that the stream is empty.
Implementation:
The implementation of the above steps given below
FileName: StreamExample3.java
import java.util.Optional; import java.util.stream.Stream; public class StreamExample3 { public static void main(String[] args) { // Example 1: Stream of Strings StreamstringStream = Stream.of("apple", "banana", "orange"); findFirstElement(stringStream); // Example 2: Stream of Integers Stream integerStream = Stream.of(10, 20, 30, 40, 50); findFirstElement(integerStream); // Example 3: Empty Stream Stream
Output:
First element of the stream: apple First element of the stream: 10 Stream is empty!