One of the most significant features introduced in Java 8 was Streams API. Since then, as Streams is on demand, later releases also focused on improving or adding the news features in the same. That’s where 2 more Stream operations came into picture with the release of Java 9. Here are we going to see two more stream operations that have been added as part of the Java 9 release i.e DropWhile & TakeWhile

TakeWhile_DropWhile_Java9_Featured_Image_Techndeck

DropWhile & TakeWhile in Java 9 with Examples...!!! Click To Tweet

In this tutorial, we are going to cover below topics:

  1. What is takeWhile & dropWhile operations in Streams?
  2. How to implement it with example?

1. What is takeWhile & dropWhile operations in Streams?

takeWhile

takeWhile expects a predicate and returns a new stream consisting only of the elements that match the given predicate. But, it’s classified among three different cases. Let’s understand all of them,

If the stream is Ordered:

It returns a stream consisting of the longest prefix of elements taken from this stream that match the given predicate. If this stream is ordered then the longest prefix is a contiguous sequence of elements of this stream that match the given predicate. 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.

If you notice above, 17 & 19 are also matching the predicate but because 16 is not matching, therefore, the returning stream won’t consider 17 and 19 and just cut off at the failing element.

If the stream is Un-Ordered:

It returns a stream consisting of a subset of elements taken from this stream that match the given predicate. If this stream is unordered, and some (but not all) elements of this stream match the given predicate, then the behavior of this operation is nondeterministic; it is free to take any subset of matching elements(which includes the empty set).

If the stream is independent of whether ordered or un0rdered (Matching all elements OR Matching none of the element):

If all elements of this stream match the given predicate then this operation takes all elements means the output is same as input, or if no elements of the stream match the given predicate then no elements are taken then the output is an empty steam.

dropWhile

dropWhile is the opposite of takeWhile. dropWhile drops the elements which is matching to the predicate instead of taking them as takeWhile. And, whenever it reaches to the element which does not match the predicate, it includes the remaining elements in the returned stream.

2. takeWhile & dropWhile implementation example

Output:

takeWhile & dropWhile are truly useful addition as part of Streams in Java 9 and can serve variety of purposes. I hope above examples could help you to get better idea on how to implement it.

DropWhile & TakeWhile in Java 9 with Examples...!!! Click To Tweet

Do you like this Post? – then check my other helpful posts:

Other Useful References: