Find the first element of a Stream in Java

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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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));
        }
    }

    chevron_right

    
    

    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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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));
        }
    }

    chevron_right

    
    

    Output:

    First Element: Geek_First
    

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.