Find the first element of a Stream in Java - GeeksforGeeks

04 Apr, 2020
Given a stream containing some elements, the task is to get the first 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:
Using Stream.reduce() Method: 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 first element.Approach:Get the stream of elements in which the first element is to be returned.To get the first element, you can use the reduce() method to ignore the second element, repeatedly, till there is no second element.Stream.reduce((first, second) -> first)
This reduces the set of elements in a Stream to a single element, which is first.Hence the only single element will be remain in the stream which is the first element.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> T    firstElementInStream(Stream<T> 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<String> 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
Using Stream findFirst() Method: The findFirst() method will returns the first element of the stream or an empty if the stream 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> T    firstElementInStream(Stream<T> 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<String> 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

Using Stream.reduce() Method: 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 first element.Approach:Get the stream of elements in which the first element is to be returned.To get the first element, you can use the reduce() method to ignore the second element, repeatedly, till there is no second element.Stream.reduce((first, second) -> first)
This reduces the set of elements in a Stream to a single element, which is first.Hence the only single element will be remain in the stream which is the first element.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> T    firstElementInStream(Stream<T> 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<String> 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

Approach:
Get the stream of elements in which the first element is to be returned.
To get the first element, you can use the reduce() method to ignore the second element, repeatedly, till there is no second element.Stream.reduce((first, second) -> first)

Stream.reduce((first, second) -> first)

This reduces the set of elements in a Stream to a single element, which is first.
Hence the only single element will be remain in the stream which is the first element.
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> T    firstElementInStream(Stream<T> 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<String> 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));    }}
First Element: Geek_First

Using Stream findFirst() Method: The findFirst() method will returns the first element of the stream or an empty if the stream 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> T    firstElementInStream(Stream<T> 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<String> 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

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()
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> T    firstElementInStream(Stream<T> 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<String> 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));    }}
First Element: Geek_First

Java-Stream-programs
Java
Java Programs
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Functional Interfaces in Java
Stream In Java
Constructors in Java
Different ways of Reading a text file in Java
Exceptions in Java
Convert a String to Character array in Java
Java Programming Examples
Convert Double to Integer in Java
Implementing a Linked List in Java using Class
How to Iterate HashMap in Java?