Method takeWhile() of Stream object in Java

This method was added from Java 9.

The parameter of this method is the Predicate interface and this method takes the elements in your Stream object from left to right, until the condition of the Predicate object is no longer fulfilled.

Example:

In the above example, the condition for getting the elements from left to right is that they must be less than 5. When the element is greater than 5, the method will stop and returns the result.

Result:

Method takeWhile() of Stream object in Java


Add Comment