Java Stream limit() with examples

 135 total views,  5 views today

Welcome to Java 8 series. In this tutorial, you will learn about java stream limit() with examples.

limit() is a part of the Stream pipeline and an intermediate operation. It belongs to the Stream interface.

  1. Java stream limit operation allows us to limit the number of elements based on the input N.It generates a new stream by limiting N elements.
  2. When the limit() N is 0, an empty stream will be returned.
  3. When the limit() N is negative, IllegalArgumentException will be thrown.

Method description

Stream<T> limit(long maxSize)

As per official documentation

Returns a stream consisting of the elements of this stream, truncated to be no longer than maxSize in length.

Since limit() is an intermediate operation it will have two characteristics.

  1. limit() is lazy loading operation. This means data will only be limited when a terminal operation is invoked on the stream.
  2. limit() operation on a stream will always create a new stream output.

Let us now see a few examples of Java 8 stream limit()

Usual way of limiting N elements using for-loop

import java.util.ArrayList;
import java.util.List;

public class StaticReference
{
    public static void main(String[] args)
    {
        int limit = 5;
        List<String> list = new ArrayList<>();
        list.add("apple");
        list.add("orange");
        list.add("banana");
        list.add("Guava");
        list.add("plums");
        list.add("peach");
        list.add("cherry");
        list.add("jackfruit");

        List<String> output = new ArrayList<>();
        for (int i = 0; i < list.size(); i++)
        {
            if (i <= limit)
            {
                output.add(list.get(i));
            }
        }
    }

}

Java stream limit() operation to limit N elements

import java.util.ArrayList;
import java.util.List;

public class StaticReference
{
    public static void main(String[] args)
    {
        List<String> list = new ArrayList<>();
        list.add("apple");
        list.add("orange");
        list.add("banana");
        list.add("Guava");
        list.add("plums");
        list.add("peach");
        list.add("cherry");
        list.add("jackfruit");

        list.stream().limit(5).forEach(System.out::println);
    }

}

Output

apple
orange
banana
Guava
plums

The below code will create an empty stream because limit is 0.

list.stream().limit(0).forEach(System.out::println);

The below code will throw IllegalArgumentException because limit is negative.

list.stream().limit(-1).forEach(System.out::println);

Conclusion

Java Stream limit() operation is mainly used for limiting N stream elements. limit() will always create a new stream and it is lazily evaluated.

References : Official documentation

Thanks for reading. Please a comment and give us a thumbs up! 

Tags :

About the Author

Rajasekar

Hey There, My name is Rajasekar and I am the author of this site. I hope you are liking my tutorials and references. Programming and learning new technologies are my passion. The ultimate idea of this site is to share my knowledge(I am still a learner :)) and help you out!. Please spread your words about us (staticreference.com) and give a thumbs up :) Feel free to contact me for any queries!.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.