🎓 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 iterate() method in Java programming and how to use it with an example.
1. Stream iterate() Method Overview
Definition:
The iterate() method in the Stream class is a static method used to create a sequential ordered Stream produced by iterative application of a function to an initial element. Each subsequent element is generated by applying the function to the previous element. There are two overloaded versions of this method: one introduced in Java 8 and another in Java 9.
Syntax:
1. static <T> Stream<T> iterate(T seed, UnaryOperator<T> f) // Java 8
2. static <T> Stream<T> iterate(T seed, Predicate<T> hasNext, UnaryOperator<T> f) // Java 9
Parameters:
- seed: the initial element.
- hasNext: a predicate to apply to elements to determine when the iteration should terminate.
- f: a function to be applied to the previous element to produce a new element.
Key Points:
- Used to create an infinite ordered Stream (Java 8) or a finite ordered Stream (Java 9) by iteratively applying a function.
- The seed parameter represents the first element in the Stream.
- The hasNext predicate (Java 9) determines when the iteration should stop.
- The f function is applied to generate subsequent elements in the Stream.
2. Stream iterate() Method Example
import java.util.function.Predicate;
import java.util.stream.Stream;
public class IterateExample {
public static void main(String[] args) {
// Java 8: Creating an infinite Stream, limit is used to limit the size of the stream
Stream<Integer> infiniteStream = Stream.iterate(1, n -> n + 1).limit(5);
System.out.println("Infinite Stream with limit:");
infiniteStream.forEach(System.out::println);
// Java 9: Creating a finite Stream using a hasNext predicate
Predicate<Integer> predicate = n -> n <= 5;
Stream<Integer> finiteStream = Stream.iterate(1, predicate, n -> n + 1);
System.out.println("Finite Stream:");
finiteStream.forEach(System.out::println);
}
}
Output:
Infinite Stream with limit: 1 2 3 4 5 Finite Stream: 1 2 3 4 5
Explanation:
In this example, we have demonstrated both overloads of the Stream::iterate() method.
1. In the first part, we used the Java 8 version of iterate(), creating an infinite Stream starting from 1 and incrementing each subsequent element by 1. We then used the limit() operation to limit the size of the Stream to 5 elements and printed these elements.
2. In the second part, we used the Java 9 version of iterate(), which takes an additional hasNext predicate. We defined a predicate that evaluates to true for values less than or equal to 5, making this a finite Stream. Then, we printed the elements of the finite Stream, which are the same as in the previous example.
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