Learnitweb

Stream dropWhile method in Java

1. Introduction

The dropWhile method was added in Stream interface in Java 9. It is a default method.

Following is the signature of dropWhile method:

default Stream<T> dropWhile​(Predicate<? super T> predicate)

It is the opposite of takeWhile method. It drops elements instead of taking them as long as predicate returns true. Once predicate returns false then rest of the Stream will be returned.

If the stream is ordered, dropWhile method returns a stream consisting of the remaining elements of this stream after dropping the longest prefix of elements that match the given predicate. If this stream is unordered, dropWhile method returns a stream consisting of the remaining elements of this stream after dropping a subset of elements that match the given predicate.

In cases where the stream is unordered and only certain elements satisfy the provided predicate, the behavior of this operation becomes non-deterministic. It retains the flexibility to discard any combination of matching elements, including the possibility of discarding all matching elements, or even none at all.

Independent of whether this stream is ordered or unordered if all elements of this stream match the given predicate then this operation drops all elements (the result is an empty stream), or if no elements of the stream match the given predicate then no elements are dropped (the result is the same as the input).

Remember that dropWhile is an intermediate operation, meaning it doesn’t modify the original stream but creates a new one reflecting the changes.

2. Examples

2.1 Example

In this example, l1 is an ordered list. Predicate checks if the element is less than 6. The dropWhile drops all the elements less than 6.

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class TakeWhileExample {
    public static void main(String[] args) {
        ArrayList<Integer> l1 = new ArrayList<Integer>();
        l1.add(1);
        l1.add(2);
        l1.add(3);
        l1.add(4);
        l1.add(5);
        l1.add(6);
        l1.add(7);
        l1.add(8);
        l1.add(9);
        l1.add(10);

        List<Integer> result = l1.stream().dropWhile(i -> i < 6).collect(Collectors.toList());
        System.out.println(result);
    }
}

Output

[6, 7, 8, 9, 10]

2.2 Example 2

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class TakeWhileExample {
    public static void main(String[] args) {
        ArrayList<Integer> l1 = new ArrayList<Integer>();
        l1.add(1);
        l1.add(2);
        l1.add(6);
        l1.add(7);
        l1.add(8);
        l1.add(4);
        l1.add(2);
        l1.add(1);
        l1.add(0);
        l1.add(11);

        List<Integer> result = l1.stream().dropWhile(i -> i < 6).collect(Collectors.toList());
        System.out.println(result);
    }
}

Output

[6, 7, 8, 4, 2, 1, 0, 11]

3. Conclusion

The dropWhile method in Java Streams provides a concise way to discard elements from the beginning of a stream as long as a specific condition holds. It continues dropping elements until the predicate evaluates to false, at which point it starts including all remaining elements in the output stream.

This method is useful for various tasks like skipping initial elements that don’t meet your criteria, filtering out specific prefixes based on conditions, and working with data streams that might have unwanted leading elements.