×
☰ See All Chapters

takeWhile in Java 9 Streams

takeWhile() method takes a predicate as an argument. A Predicate is Boolean expression which evaluates to either true or false. Predicate will be evaluated against the element from the stream. If predicate evaluates to true it will be collected, if predicate evaluates to false, it stops processing further (it stops taking elements further). takeWhile continues processing the stream till the predicate is evaluated to true. Once predicate is evaluated to false it stops. All the elements which evaluated to true till then will be collected.

When you read the javadocs you see that takeWhile() method behave differently for ordered and unordered streams. Yes, this is of course correct, but when you may get confuse when you read that. We made it simple for you. Don’t compare the two different cases of ordered and unordered. Just follow the below description and understand the takeWhile operation.

takeWhile Example

package com.java4coding.test;

 

import java.util.ArrayList;

import java.util.List;

import java.util.stream.Collectors;

import java.util.stream.Stream;

 

public class Test {

        public static void main(String[] args) {

                Test test = new Test();

                test.takeWhileDemo();

 

        }

 

        public void takeWhileDemo() {

                List<Integer> list = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10).takeWhile(i -> (i % 2 == 0))

                                .collect(Collectors.toList());

                System.out.println(list);

        }

}

 

Output:

[]

The above example has to evaluate the predicate (i % 2 == 0) starting from 1 and till 10 one by one, When predicate is evaluated against the fist element 1 it evaluates to false (1 % 2 == 0). Hence it stops processing further.

The below takeWhile() java 7 equivalent program will make you understand takeWhile() method clearly.

takeWhile implementation in java 8 and below

package com.java4coding.test;

 

import java.util.ArrayList;

import java.util.List;

import java.util.stream.Collectors;

import java.util.stream.Stream;

 

public class Test {

        public static void main(String[] args) {

                Test test = new Test();

                test.takeWhileEquivalent();

        }

 

        public void takeWhileEquivalent() {

                List<Integer> result = new ArrayList<>();

                List<Integer> list = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10).collect(Collectors.toList());

                for (int i = 0; i < list.size(); i++) {

                        if (list.get(i) % 2 == 0) {

                                result.add(list.get(i));

                        } else {

                                break;

                        }

                }

                System.out.println(result);

        }

}

Consider the below one more example for takeWhile() method

package com.java4coding.test;

 

import java.util.List;

import java.util.stream.Collectors;

import java.util.stream.Stream;

 

public class Test {

        public static void main(String[] args) {

                Test test = new Test();

                test.takeWhileDemo();

        }

 

        public void takeWhileDemo() {

                List<Integer> list = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10).takeWhile(i -> (i < 5))

                                .collect(Collectors.toList());

                System.out.println(list);

        }

 

}

Output:

[1, 2, 3, 4]

All the elements 1, 2, 3, 4 evaluate to true for the predicate (i < 5) and hence all the elements are collected. When the predicate (i < 5) evaluates to false, it stops processing further.


All Chapters
Author