Java 9 Stream API Improvement
In Java 9, Stream API has improved and new methods are added to the Stream interface. These methods are tabled below.
Java Stream takeWhile() Method
Stream takeWhile method takes each element that matches its predicate. It stops when it get unmatched element. It returns a subset of elements that contains all matched elements, other part of stream is discarded.
Java Stream takeWhile() Method Example 1
In this example, we have a list of integers and picks up even values by using takewhile method.
This example returns an empty list because it fails at first list element, and takewhile stops here.
Java Stream takeWhile() Method Example 2