Open In App

Stream takeWhile() method in Java with examples

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

The takeWhile(java.util.function.Predicate) method returns a stream of the remaining elements of this stream after taken the longest prefix of elements that match the given predicate if the stream is ordered else a stream of a subset of elements taken from this stream that match the given predicate.
In the case of the ordered stream, the longest prefix is a contiguous sequence of elements of this stream that match the predicate passed as a parameter to this method. The first element of the sequence is the first element of this stream, and the element immediately following the last element of the sequence does not match the given predicate.
In the case of the unordered stream, some elements of this stream match the given predicate and behaviour of this operation becomes nondeterministic; so this method is free to take any subset of matching elements.
When all elements of this stream match the given predicate then this method will take all elements and the result is same as input stream irrespective of the order of Stream and when no elements of the stream match the given predicate then no elements are taken and the result is empty.

Syntax:

default Stream<T> takeWhile(Predicate<T> predicate)

Parameters: This method accepts a single parameter predicate which is a non-interfering, stateless predicate to apply to elements to determine the longest prefix of elements.

Return value: This method returns the new stream.

Below programs illustrate takeWhile(java.util.function.Predicate) method:
Program 1:




// Java program to demonstrate
// Stream.takeWhile method
  
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class GFG {
  
    public static void main(String[] args)
    {
  
        // create a stream of numbers from 1 to 10
        Stream<Integer> stream
            = Stream.of(4, 4, 4, 5, 6, 7, 8, 9, 10);
  
        // apply takeWhile to take all the numbers
        // matches passed predicate
        List<Integer> list
            = stream.takeWhile(number -> (number / 4 == 1))
                  .collect(Collectors.toList());
  
        // print list
        System.out.println(list);
    }
}


The output printed on console of IDE is shown below.
Output:

Program 2:




// Java program to demonstrate
// Stream.takeWhile method
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class GFG {
  
    public static void main(String[] args)
    {
  
        // create a stream of names
        Stream<String> stream
            = Stream.of("aman", "amar", "suraj",
                        "suvam", "Zahafuj");
  
        // apply takeWhile to take all the names
        // matches passed predicate
        List<String> list
            = stream.takeWhile(name -> (name.charAt(0) == 'a'))
                  .collect(Collectors.toList());
  
        // print list
        System.out.println(list);
    }
}


The output printed on console is shown below.
Output:

References: https://docs.oracle.com/javase/10/docs/api/java/util/stream/Stream.html#takeWhile(java.util.function.Predicate)



Last Updated : 25 Apr, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads