Stream skip() method in Java with examples Last Updated : 06 Dec, 2018 Comments Improve Suggest changes Like Article Like Report 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. Syntax : 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. Java // Java code for skip() function import java.util.*; class GFG { // Driver code public static void main(String[] args) { // Creating a list of integers List<Integer> list = new ArrayList<Integer>(); // adding elements in the list list.add(-2); list.add(0); list.add(2); list.add(4); list.add(6); list.add(8); list.add(10); list.add(12); list.add(14); list.add(16); // setting the value of N as 4 int limit = 4; int count = 0; Iterator<Integer> it = list.iterator(); // Iterating through the list of integers while (it.hasNext()) { it.next(); count++; // Check if first four i.e, (equal to N) // integers are iterated. if (count <= limit) { // If yes then remove first N elements. it.remove(); } } System.out.print("New stream is : "); // Displaying new stream for (Integer number : list) { System.out.print(number + " "); } } } Output : New stream is : 6 8 10 12 14 16 Application : Java // Java code for skip() function import java.util.stream.Stream; import java.util.ArrayList; import java.util.List; class gfg{ // Function to skip the elements of stream upto given range, i.e, 3 public static Stream<String> skip_func(Stream<String> ss, int range){ return ss.skip(range); } // Driver code public static void main(String[] args){ // list to save stream of strings List<String> arr = new ArrayList<>(); arr.add("geeks"); arr.add("for"); arr.add("geeks"); arr.add("computer"); arr.add("science"); Stream<String> str = arr.stream(); // calling function to skip the elements to range 3 Stream<String> sk = skip_func(str,3); sk.forEach(System.out::println); } } Output : computer science 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. Comment More infoAdvertise with us Next Article Stream skip() method in Java with examples S Sahil_Bansall Follow Improve Article Tags : Misc Java Java - util package Java-Functions java-stream Java-Stream interface +2 More Practice Tags : JavaMisc Similar Reads Stream peek() Method in Java with Examples In Java, Stream provides an powerful alternative to process data where here we will be discussing one of the very frequently used methods named peek() which being a consumer action basically returns a stream consisting of the elements of this stream, additionally performing the provided action on ea 2 min read Optional stream() method in Java with examples The stream() method of java.util.Optional class in Java is used to get the sequential stream of the only value present in this Optional instance. If there is no value present in this Optional instance, then this method returns returns an empty Stream. Syntax: public Stream<T> stream() Paramete 2 min read Stream count() method in Java with examples long count() returns the count of elements in the stream. This is a special case of a reduction (A reduction operation takes a sequence of input elements and combines them into a single summary result by repeated application of a combining operation). This is a terminal operation i.e, it may travers 2 min read Stream mapToInt() in Java with examples Stream mapToInt(ToIntFunction mapper) returns an IntStream consisting of the results of applying the given function to the elements of this stream. Stream mapToInt(ToIntFunction mapper) is an intermediate operation. These operations are always lazy. Intermediate operations are invoked on a Stream in 2 min read OptionalInt stream() method in Java with examples The stream() method help us to get value contain by OptionalInt as IntStream. If a value is present, method returns a sequential IntStream containing only that value, otherwise returns an empty IntStream. Syntax: public IntStream stream() Parameters: This method accepts nothing. Return value: This m 1 min read BitSet stream() Method in Java with Examples The stream() method of Java BitSet class is used to return a stream of indices for every bit contained in the BitSet. The indices are returned in increasing order. The size of the stream is the number of bits in the set state of the BitSet, which is equal to the value returned by the cardinality() m 2 min read Stream forEach() method in Java with examples Stream forEach(Consumer action) performs an action for each element of the stream. Stream forEach(Consumer action) is a terminal operation i.e, it may traverse the stream to produce a result or a side-effect. Syntax : void forEach(Consumer<? super T> action) Where, Consumer is a functional int 2 min read OptionalLong stream() method in Java with examples The stream() method help us to get Long value contain by OptionalLong as LongStream.If a value is present, method returns a sequential LongStream containing only that value, otherwise returns an empty LongStream. Syntax: public LongStream stream() Parameters: This method accepts nothing. Return valu 1 min read PrintStream println() method in Java with Examples The println() method of PrintStream Class in Java is used to break the line in the stream. This method do not accepts any parameter or return any value. Syntax: public void println() Parameters: This method do not accepts any parameter. Return : This method do not returns any value. Below methods il 2 min read PushbackInputStream read() method in Java with Examples The read() method of PushbackInputStream class in Java is of two types: The read() method of PushbackInputStream class in Java is used to read the next byte of data from the input stream. This method returns the read byte from the input stream in the form of an integer. Syntax: public int read() thr 4 min read Like