🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
In this guide, you will learn about the Stream dropWhile() method in Java programming and how to use it with an example.
1. Stream dropWhile() Method Overview
Definition:
The dropWhile() method of the Stream interface in Java is used to discard elements from the stream as long as the given predicate holds true. Once an element fails to match the predicate, the operation stops, and the elements after that are included in the resulting stream.
Syntax:
Stream<T> dropWhile(Predicate<? super T> predicate)
Parameters:
- predicate: A non-interfering, stateless predicate to apply to elements of the stream, which returns true if the element should be discarded.
Key Points:
- Introduced in Java 9.
- This is a stateful intermediate operation.
- For ordered streams, it drops the longest prefix of elements that match the predicate.
- If the stream is unordered, some (or none) elements will be dropped until the predicate returns false.
2. Stream dropWhile() Method Example
import java.util.stream.Stream;
public class DropWhileExample {
public static void main(String[] args) {
// Creating a Stream of integers
Stream<Integer> numberStream = Stream.of(2, 4, 6, 8, 9, 10, 12);
// Using dropWhile() to discard elements as long as they are even
Stream<Integer> resultStream = numberStream.dropWhile(number -> number % 2 == 0);
// Displaying the result
resultStream.forEach(System.out::println);
}
}
Output:
9 10 12
Explanation:
In the example, we have a Stream of integers. We use the dropWhile() method to discard elements from the stream as long as they are even.
As soon as the method encounters the number 9, which is not even, the operation stops, and the remaining elements are included in the resulting stream.
The output of the program demonstrates this, showing the numbers in the original stream that appear after the first odd number.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment