Broadcast Stream #2

Learn to work with two more Broadcast Stream methods: takeWhile() & skipWhile(), along with modifying Streams.

Using the takeWhile() method

The takeWhile() method functions the same as the take() method when a condition is fulfilled.

Let’s add conditions that pick/take the elements which are positive but less than 3. There are two such numbers in [1, 2, 3, 4, 5] that will be picked; 1 and 2. takeWhile() will check the condition and will print these 2 numbers on the console. It will print the numbers that are less than 3 and greater than 0.

Get hands-on with 1400+ tech skills courses.