🎓 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
The limit() method in Java, part of the java.util.stream.Stream interface, is used to truncate a stream to a given length. This method is useful when you need to process only a specific number of elements from a stream.
Table of Contents
- Introduction
limit()Method Syntax- Understanding
limit() - Examples
- Basic Usage
- Using
limit()with Filtered Streams
- Real-World Use Case
- Conclusion
Introduction
The limit() method returns a stream consisting of the elements of the original stream, truncated to be no longer than the specified size. This method is an intermediate operation, meaning it returns a new stream and does not modify the original stream.
limit() Method Syntax
The syntax for the limit() method is as follows:
Stream<T> limit(long maxSize)
Parameters:
maxSize: The number of elements the resulting stream should be limited to.
Returns:
- A new
Streamconsisting of the elements of the original stream, truncated to the specified length.
Throws:
IllegalArgumentException: IfmaxSizeis negative.
Understanding limit()
The limit() method allows you to limit the number of elements in a stream to a specified maximum size. This is particularly useful when you only need to process a subset of the elements in a stream.
Examples
Basic Usage
To demonstrate the basic usage of limit(), we will create a Stream and use limit() to truncate it to a specific number of elements.
Example
import java.util.stream.Stream;
public class LimitExample {
public static void main(String[] args) {
Stream<String> stream = Stream.of("apple", "banana", "cherry", "date", "elderberry");
// Use limit() to truncate the stream to 3 elements
Stream<String> limitedStream = stream.limit(3);
// Print the limited elements
limitedStream.forEach(System.out::println);
}
}
Output:
apple
banana
cherry
Using limit() with Filtered Streams
This example shows how to use limit() in combination with other stream operations, such as filtering.
Example
import java.util.stream.Stream;
public class LimitWithFilterExample {
public static void main(String[] args) {
Stream<String> stream = Stream.of("apple", "banana", "cherry", "date", "elderberry");
// Filter elements that start with 'a' or 'b', and limit the result to 2 elements
Stream<String> limitedStream = stream.filter(s -> s.startsWith("a") || s.startsWith("b"))
.limit(2);
// Print the limited elements
limitedStream.forEach(System.out::println);
}
}
Output:
apple
banana
Real-World Use Case
Pagination
In real-world applications, the limit() method can be used to implement pagination by combining it with skip() to process specific pages of data.
Example
import java.util.stream.Stream;
public class PaginationExample {
public static void main(String[] args) {
Stream<String> stream = Stream.of("apple", "banana", "cherry", "date", "elderberry",
"fig", "grape", "honeydew");
int page = 2;
int pageSize = 3;
// Use skip() to skip the elements of previous pages, and limit() to get the elements of the current page
Stream<String> pageStream = stream.skip((page - 1) * pageSize)
.limit(pageSize);
// Print the elements of the current page
pageStream.forEach(System.out::println);
}
}
Output:
date
elderberry
fig
Conclusion
The Stream.limit() method is used to truncate a stream to a specified maximum length. This method is particularly useful for scenarios where you need to process only a specific number of elements from a stream. By understanding and using this method, you can efficiently manage and process streams of values in your Java applications, ensuring that you work with only the required number of elements.
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