Java program to find the first element of a stream

Given a flow into containing some elements, the challenge is to get the number one 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 in a Stream:

  1. Using Stream.reduce() Method: 
    The reduce technique works on two elements inside the movement and returns the detail as per the desired circumstanceconsequently this method may be used to reduce the stream so that it consists of handiest the first element.

Approach:

  • Get the circulate of elements wherein the first element is to be lower back.
  • To get the first elementyou could use the reduce() approach to disregard the second elementagain and againtill there is no second element.

 

Stream.reduce((first, second) -> first)
  • This reduces the set of factors in a movement to a single detailthat's first.
  • consequently the simplest unmarried element will remain within the move that is the primary detail.

Below is the implementation of the above approach:

Example:

 
// Java program to find first
// element of a Stream in Java

import java.util.*;
import java.util.stream.*;

public class GFG {

	// Function to find the
	// first_elements in a Stream
	public static  T
	firstElementInStream(Stream stream)
	{
		T first_element
			= stream

				// reduce() method reduces the Stream
				// to a single element, which is first.
				.reduce((first, second) -> first)

				// if stream is empty
				// null is returned
				.orElse(null);

		return first_element;
	}

	// Driver code
	public static void main(String[] args)
	{

		Stream stream
			= Stream.of("Geek_First", "Geek_2",
						"Geek_3", "Geek_4",
						"Geek_Last");

		// Print the first element of a Stream
		System.out.println(
			"First Element: "
			+ firstElementInStream(stream));
	}
}

Output:

First Element: Geek_First

2. Using Stream findFirst() Method:  

The findFirst() approach will go back the first detail of the circulate or an empty if the movement is empty.
 

Approach:

 

  • Get the stream of elements in which the first element is to be returned.
  • To get the first element, you can directly use the findFirst() method.

 

Stream.findFirst()
  • This will return the first element of the stream.

Below is the implementation of the above approach:

Example:

 
// Java program to find first
// element of a Stream in Java

import java.util.*;
import java.util.stream.*;

public class GFG {

	// Function to find the
	// first_elements in a Stream
	public static  T
	firstElementInStream(Stream stream)
	{
		T first_element
			= stream

				// findFirst() method returns
				// the first element of stream
				.findFirst()

				// if stream is empty
				// null is returned
				.orElse(null);

		return first_element;
	}

	// Driver code
	public static void main(String[] args)
	{

		Stream stream
			= Stream.of("Geek_First", "Geek_2",
						"Geek_3", "Geek_4",
						"Geek_Last");

		// Print the first element of a Stream
		System.out.println(
			"First Element: "
			+ firstElementInStream(stream));
	}
}

Output:

First Element: Geek_First

 

Submit Your Programming Assignment Details