This method was added from Java 9.
1 |
default Stream<T> dropWhile(Predicate<? super T> predicate) |
This method also has the parameter Predicate interface and its function is the opposite of the takeWhile() method. This method also passes each element in your Stream object from left to right and ignores all elements that satisfy the condition. Once the condition is no longer fulfilled, it will take all the remaining elements to return.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 |
package com.huongdanjava.javastream; import java.util.stream.Stream; public class Example { public static void main(String[] args) { Stream.of(3,2,4,1,4,6,7,8,9,10) .dropWhile(i -> i < 5) .forEach(System.out::println); } } |
In the above example, the dropWhile() method will ignore all elements smaller than 5 and when an element larger than 5, it will return all the remaining elements including this element.
Result: