Stream dropWhile() method in Java with examples Last Updated : 11 Apr, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report The dropWhile(java.util.function.Predicate) method returns two different types of stream depending upon whether the stream is ordered or not. If the stream is ordered then a stream of the remaining elements of this stream after dropping the longest prefix of elements that match the given predicate is returned by method else a stream consisting of the remaining elements of this stream after dropping a subset of elements that match the given predicate. In the case of the ordered stream, the longest prefix is a contiguous sequence of elements of this stream that match the predicate passed as a parameter to this method. The first element of the sequence is the first element of this stream, and the element immediately following the last element of the sequence does not match the given predicate. In the case of the unordered stream, some elements of this stream match the given predicate and the behavior of this operation becomes nondeterministic; so this method is free to drop any subset of matching elements. There may be some cases when all elements of this stream match the given predicate then this method will drop all elements and the result is an empty stream irrespective of order of Stream and when no elements of the stream match the given predicate then no elements are dropped and the result is the same as the input stream. Syntax: default Stream<T> dropWhile(Predicate<T> predicate) Parameters: This method accepts a single parameter predicate which is a non-interfering, stateless predicate to apply to elements to determine the longest prefix of elements. Return value: This method returns the new stream. Below programs illustrate dropWhile(java.util.function.Predicate) method: Program 1:Â Java // Java program to demonstrate // Stream.dropWhile method import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; public class GFG { public static void main(String[] args) { // create a stream of numbers from 1 to 10 Stream<Integer> stream = Stream.of(4, 4, 4, 5, 6, 7, 8, 9, 10); // apply dropWhile to drop all the numbers // matches passed predicate List<Integer> list = stream.dropWhile(number -> (number / 4 == 1)) .collect(Collectors.toList()); // print list System.out.println(list); } } The output printed on console of IDE is shown below. Output: Program 2:Â Java // Java program to demonstrate // Stream.dropWhile method import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; public class GFG { public static void main(String[] args) { // create a stream of names Stream<String> stream = Stream.of("aman", "amar", "suraj", "suvam", "Zahafuj"); // apply dropWhile to drop all the names // matches passed predicate List<String> list = stream.dropWhile(name -> (name.charAt(0) == 'a')) .collect(Collectors.toList()); // print list System.out.println(list); } } The output printed on console is shown below. Output: References: https://docs.oracle.com/javase/10/docs/api/java/util/stream/Stream.html#dropWhile(java.util.function.Predicate) Comment More infoAdvertise with us Next Article Stream dropWhile() method in Java with examples A AmanSingh2210 Follow Improve Article Tags : Java Java - util package Java-Functions java-stream Practice Tags : Java Similar Reads Stream count() method in Java with examples long count() returns the count of elements in the stream. This is a special case of a reduction (A reduction operation takes a sequence of input elements and combines them into a single summary result by repeated application of a combining operation). This is a terminal operation i.e, it may travers 2 min read Stream peek() Method in Java with Examples In Java, Stream provides an powerful alternative to process data where here we will be discussing one of the very frequently used methods named peek() which being a consumer action basically returns a stream consisting of the elements of this stream, additionally performing the provided action on ea 2 min read Stream skip() method in Java with examples Prerequisite : Streams in java The skip(long N) is a method of java.util.stream.Stream object. This method takes one long (N) as an argument and returns a stream after removing first N elements. skip() can be quite expensive on ordered parallel pipelines, if the value of N is large, because skip(N) 3 min read Reader close() method in Java with Examples The close() method of Reader Class in Java is used to close the stream and release the resources that were busy in the stream, if any. This method has following results: If the stream is open, it closes the stream releasing the resources If the stream is already closed, it will have no effect. If an 2 min read Stream forEach() method in Java with examples Stream forEach(Consumer action) performs an action for each element of the stream. Stream forEach(Consumer action) is a terminal operation i.e, it may traverse the stream to produce a result or a side-effect. Syntax : void forEach(Consumer<? super T> action) Where, Consumer is a functional int 2 min read Like