Prerequisite : Streams in java
The skip(long N) is a method of java.util.stream.Stream object. This method takes one long (N) as an argument and returns a stream after removing first N elements. skip() can be quite expensive on ordered parallel pipelines, if the value of N is large, because skip(N) is constrained to skip the first N elements in the encounter order and not just any n elements.
Note : If a stream contains less than N elements, then an empty stream is returned.
Stream<T> skip(long N) Where N is the number of elements to be skipped and this function returns new stream as output.
Exception : If the value of N is negative, then IllegalArgumentException is thrown by the function.
Example 1 : Implementation of skip function.
New stream is : 6 8 10 12 14 16
Difference between limit() and skip() :
- The limit() method returns a reduced stream of first N elements but skip() method returns a stream of remaining elements after skipping first N elements.
- limit() is a short-circuiting stateful intermediate operation i.e, when processed with an infinite input, it may produce a finite stream as a result without processing the entire input but skip() is a stateful intermediate operation i.e, it may need to process the entire input before producing a result.
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Scanner skip() method in Java with Examples
- Reader skip(long) method in Java with Examples
- CharArrayReader skip(long) method in Java with Examples
- StringReader skip(long) method in Java with Examples
- ByteArrayInputStream skip() method in Java with Examples
- PushbackReader skip(long) method in Java with Examples
- BufferedInputStream skip(long) method in Java with Examples
- BufferedReader skip(long) method in Java with Examples
- PushbackInputStream skip() method in Java with Examples
- Difference between Stream.of() and Arrays.stream() method in Java
- Character Stream Vs Byte Stream in Java
- IntStream skip() in Java
- DoubleStream skip() in Java
- LongStream skip() in Java
- foreach() loop vs Stream foreach() vs Parallel Stream foreach()
- Stream.max() method in Java with Examples
- Stream min() method in Java with Examples
- Stream generate() method in Java with examples
- Stream count() method in Java with examples
- Stream forEach() method in Java with examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.