×
☰ See All Chapters

dropWhile in Java 9 Streams

dropWhile() 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 dropped, if predicate evaluates to false, it stops processing further (it stops dropping elements further,  so it takes all rest of the elements). dropWhile continues process the stream till the predicate is evaluated to true. Once predicate is evaluated to false it stops dropping elements. All the elements which evaluated to true till then will be dropped. The element which evaluated to false and rest of the elements will be taken.

When you read the javadocs you see that dropWhile() method behave differently for ordered and unordered streams. Yes, this is of course correct, but when you read that you may get confuse. 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 dropWhile operation.

dropWhile Example

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.dropWhileDemo();

        }

 

        public void dropWhileDemo() {

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

                                .collect(Collectors.toList());

                System.out.println(list);

        }

}

Output:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

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. It will not drop any element and will take all rest of the elements.

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

doWhile 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.dropWhileEquivalent();

        }

        public void dropWhileEquivalent() {

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

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

                boolean stopDropping = false;

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

                        if (!stopDropping) {

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

                                        //do nothing

                                } else {

                                        stopDropping = true;

                                }

                        }

                        if (stopDropping) {

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

                        }

                }

                System.out.println(result);

        }

}

Consider the below one more example for doWhile() 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.dropWhileDemo();

        }

 

        public void dropWhileDemo() {

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

                                .collect(Collectors.toList());

                System.out.println(list);

        }

 

}

Output:

[5, 6, 7, 8, 9, 10]

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


All Chapters
Author