Java 8 Stream limit & skip method Example

7 years ago Lalit Bhagtani 0

In this tutorial, we will learn about Java 8 stream limit and skip method.

java 8 stream limit

limit() method :-

This method takes one long ( N ) as an argument and return a stream of length N, after truncating it.

Syntax :-  Stream<T> limit(long n)

Reference :-  limit() method JavaDocs

Problem Statement :-

You have given a list of integers, remove all the integers excluding first three and print them.

Java 7 :- To implement this, first iterate through the list of integers. Check if first three integers are iterated, if yes then remove all the remaining integers.

  public static void main(String[] args){
 
    List<Integer> numList = new ArrayList<Integer>();
    numList.add(2); numList.add(4);
    numList.add(12); numList.add(7); 
    numList.add(16); numList.add(17); 
    numList.add(19); numList.add(20); 
    numList.add(34); numList.add(56); 
 
    int limit = 3;
    int count = 0;
    Iterator<Integer> iterate = numList.iterator();
    while(iterate.hasNext()){
      iterate.next();
      count++;
      if(count > limit){
        iterate.remove();
      }
    }
    for(Integer number : numList){
      System.out.println(number);
    }
  }

Java 8 Stream limit :- Implementation using streams API.

  public static void main(String[] args){
 
    Integer[] num = new Integer[]{2,4,12,7,16,17,19,20,34,56};
    List<Integer> numList = Arrays.asList(num);
 
    List<Integer> tempList = numList.stream().limit(3).collect(Collectors.toList());
    tempList.forEach(e -> System.out.println(e));
 
  }  

Result :- 

2
4
12

skip() method :-

This method takes one long ( N ) as an argument and return a stream, after removing first N elements. If a stream contains less element than N, an empty stream is returned.

Syntax :-  Stream<T> skip(long n)

Reference :-  skip() method JavaDocs

Problem Statement :-

You have given a list of integers, remove first three integers and print rest of them.

Java 7 :- To implement this, first iterate through the list of integers. Remove first three integers and print rest of them.

  public static void main(String[] args){
 
    List<Integer> numList = new ArrayList<Integer>();
    numList.add(2); numList.add(4);
    numList.add(12); numList.add(7); 
    numList.add(16); numList.add(17); 
    numList.add(19); numList.add(20); 
    numList.add(34); numList.add(56); 
 
    int limit = 3;
    int count = 0;
    Iterator<Integer> iterate = numList.iterator();
    while(iterate.hasNext()){
      iterate.next();
      count++;
      if(count <= limit){
        iterate.remove();
      }
    }
    for(Integer number : numList){
      System.out.println(number);
    }
  }

Java 8 Stream skip :- Implementation using streams API.

  public static void main(String[] args){
 
    Integer[] num = new Integer[]{2,4,12,7,16,17,19,20,34,56};
    List<Integer> numList = Arrays.asList(num);
 
    List<Integer> tempList = numList.stream().skip(3).collect(Collectors.toList());
    tempList.forEach(e -> System.out.println(e));
 
  }  

Result :- 

7
16
17
19
20
34
56

That’s all for Java 8 Stream limit & skip method Example. If you liked it, please share your thoughts in comments section and share it with others too.