Skip to content
Related Articles

Related Articles

Improve Article

Find the first element of a Stream in Java

  • Last Updated : 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:



  1. 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
    
  2. 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
    

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :