Java LongStream limit() Method

The limit() method in Java, part of the java.util.stream.LongStream interface, is used to truncate the stream to contain no more than a specified number of elements. This method is useful when you need to restrict the size of the stream to a specific number of elements.

Table of Contents

  1. Introduction
  2. limit() Method Syntax
  3. Understanding limit()
  4. Examples
    • Basic Usage
    • Using limit() with Other Stream Operations
  5. Real-World Use Case
  6. Conclusion

Introduction

The limit() method returns a stream consisting of the elements of the original stream, truncated to be no longer than the specified length. 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:

LongStream limit(long maxSize)

Parameters:

  • maxSize: The number of elements the resulting stream should be limited to.

Returns:

  • A new LongStream consisting of the elements of the original stream, truncated to the specified length.

Throws:

  • IllegalArgumentException if maxSize is negative.

Understanding limit()

The limit() method allows you to restrict the number of elements in a stream. It is useful for scenarios where you need to process only a specific number of elements from a potentially large stream.

Examples

Basic Usage

To demonstrate the basic usage of limit(), we will create a LongStream and use limit() to truncate it to a specific number of elements.

Example

import java.util.stream.LongStream;

public class LimitExample {
    public static void main(String[] args) {
        LongStream stream = LongStream.of(1L, 2L, 3L, 4L, 5L);

        // Use limit() to truncate the stream to 3 elements
        LongStream limitedStream = stream.limit(3);

        // Print the elements of the limited stream
        limitedStream.forEach(System.out::println);
    }
}

Output:

1
2
3

Using limit() with Other Stream Operations

This example shows how to use limit() in combination with other stream operations, such as filtering.

Example

import java.util.stream.LongStream;

public class LimitWithFilterExample {
    public static void main(String[] args) {
        LongStream stream = LongStream.of(10L, 20L, 30L, 40L, 50L);

        // Use limit() to truncate the stream to 2 elements after filtering
        LongStream limitedStream = stream.filter(n -> n > 20).limit(2);

        // Print the elements of the limited stream
        limitedStream.forEach(System.out::println);
    }
}

Output:

30
40

Real-World Use Case

Limiting the Number of Processed Transactions

In real-world applications, the limit() method can be used to limit the number of processed transactions from a stream of transaction values.

Example

import java.util.stream.LongStream;

public class LimitTransactionsExample {
    public static void main(String[] args) {
        LongStream transactionAmounts = LongStream.of(1000L, 2000L, 1500L, 3000L, 2500L);

        // Use limit() to process only the first 3 transactions
        LongStream limitedTransactions = transactionAmounts.limit(3);

        // Print the limited transaction amounts
        limitedTransactions.forEach(amount -> System.out.println("Transaction Amount: " + amount));
    }
}

Output:

Transaction Amount: 1000
Transaction Amount: 2000
Transaction Amount: 1500

Conclusion

The LongStream.limit() method is used to truncate the stream to contain no more than a specified number of elements. This method is particularly useful for restricting the size of the stream to a specific number of elements. By understanding and using this method, you can efficiently manage and process streams of values in your Java applications, ensuring that only a limited number of elements are processed as needed.

Continue Learning with Java Guides

Want to take your programming skills to the next level? Explore my practical Udemy courses on Java, Spring Boot, Microservices, Full-Stack Development, Docker, and Generative AI. You can also follow my structured learning roadmaps to become a professional developer.

By Ramesh Fadatare - Udemy Instructor Partner

Comments