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.
- Scanner skip() method in Java with Examples
- ByteArrayInputStream skip() method in Java with Examples
- StringReader skip(long) method in Java with Examples
- PushbackReader skip(long) method in Java with Examples
- CharArrayReader skip(long) method in Java with Examples
- Reader skip(long) method in Java with Examples
- Stream min() method in Java with Examples
- Stream.max() method in Java with Examples
- Stream count() method in Java with examples
- Stream generate() method in Java with examples
- BitSet stream() Method in Java with Examples
- OptionalDouble stream() method in Java with examples
- Optional stream() method in Java with examples
- Stream forEach() method in Java with examples
- Stream forEachOrdered() 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 email@example.com. 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.