IntStream skip() method in Java

Java 8Object Oriented ProgrammingProgramming

The skip() method of the IntStream class in Java returns a stream consisting of the remaining elements of this stream after discarding the first n elements of the stream.

The syntax is as follows

IntStream skip(long n)

Here, n is the number of elements to skip. The method returns the new stream.

Create an IntStream and add some elements within a range using range() method

IntStream intStream = IntStream.range(20, 40);

Now to skip some elements and only display the rest of them, use the skip() method

intStream.skip(15)

The following is an example to implement IntStream skip() method in Java. It skips 15 elements since we have set the parameter value of the skip() method as 15

Example

 Live Demo

import java.util.*;
import java.util.stream.IntStream;
public class Demo {
   public static void main(String[] args) {
      IntStream intStream = IntStream.range(20, 40);
      intStream.skip(15).forEach(System.out::println);
   }
}

Output

35
36
37
38
39
raja
Published on 27-Mar-2019 12:46:07
Advertisements