🎓 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 takeWhile() method in Java programming and how to use it with an example.
1. Stream takeWhile() Method Overview
Definition:
The takeWhile() method of the Stream interface in Java is used to return 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 not included in the resulting stream.
Syntax:
Stream<T> takeWhile(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 included in the result.
Key Points:
- Introduced in Java 9.
- This is a short-circuiting stateful intermediate operation.
- The operation stops as soon as the predicate returns false or the end of the stream is reached.
- If the stream is unordered, any subset of the elements satisfying the predicate may be returned.
- For ordered streams, the returned stream is also ordered.
2. Stream takeWhile() Method Example
import java.util.stream.Stream;
public class TakeWhileExample {
public static void main(String[] args) {
// Creating a Stream of integers
Stream<Integer> numberStream = Stream.of(2, 4, 6, 8, 9, 10, 12);
// Using takeWhile() to return elements as long as they are even
Stream<Integer> resultStream = numberStream.takeWhile(number -> number % 2 == 0);
// Displaying the result
resultStream.forEach(System.out::println);
}
}
Output:
2 4 6 8
Explanation:
In the example, we have a Stream of integers. We use the takeWhile() method to take 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 not included in the resulting stream.
The output of the program demonstrates this, showing only the even numbers before the first odd number in the original stream.
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