1. Overview
In this article, You'll learn how to use stream skip() method of java 8 and how to skip the first n objects from stream.
2. Java 8 Stream skip() Syntax
skip() method is an intermediate operation that returns Stream of objects.
Stream<T> skip(long n)
This method returns a stream consisting of the remaining elements of this stream after discarding the first n elements of the stream. If this stream contains fewer than n elements then an empty stream will be returned.
skip() method takes a long parameter and returns Stream.
3. Java 8 Stream skip() Method
You've to remember the following points before using the skip() method.
A) skip() method is a stateful intermediate operation similar to the sorted(), filter() methods.
B) Returns a Stream after discarding first n elements from the stream.
C) If the stream has less than the number n given, it returns an empty sequential stream by calling the Stream.empty() method.
D) This is good for only sequential streams and must avoid in parallel streams for higher values of n.
E) If you use unordered infinite streams or unordered() streams may result in producing better results.
F) It just skips the first n values from the stream that the order is present in the underlying base stream or previous intermediate operation call output.
G) n value can not be negative. If n is negative, it throws java.lang.IllegalArgumentException.
4. Java 8 Stream skip() Example 1
Understand the skip() example program to skip the first 10 numbers from infinite numbers series and collect the next 20 values using limit() method.
If you are using infinite streams then you must have to use the limit() method. Otherwise, stream will end up in memory-related issues.
package com.javaprogramto.java8.skip;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Skip10ValuesExample {
public static void main(String[] args) {
Stream<Integer> numnerSeries = Stream.iterate(1, i -> i+ 1);
List<Integer> next20Numbers = numnerSeries.skip(10).limit(20).collect(Collectors.toList());
System.out.println("20 Numbers after skipping first 10 numbers");
next20Numbers.forEach(number -> System.out.println(number));
}
}
Output:
20 Numbers after skipping first 10 numbers 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
5. Java 8 Stream skip() Example 2
Example to skip the first 5 even numbers from List.
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Skip5EvenNumbersExample {
public static void main(String[] args) {
Stream<Integer> evenNumbers = Stream.of(2, 4, 5, 8, 10, 12, 14, 16, 18, 20);
List<Integer> next20Numbers = evenNumbers
.skip(5)
.collect(Collectors.toList());
System.out.println("skipping first 5 even Numbers");
next20Numbers.forEach(number -> System.out.println(number));
}
}
Output:
skipping first 5 even Numbers 12 14 16 18 20
6. Java 8 Stream skip() Example 3
What is the output if a negative index is passed to skip() method.
public class SkipNegativeIndexExample {
public static void main(String[] args) {
Stream<Integer> numbers = Stream.of(1, 2, 3);
List<Integer> expectingError = numbers
.skip(-25)
.collect(Collectors.toList());
System.out.println("skipping first 5 even Numbers");
expectingError.forEach(number -> System.out.println(number));
}
}
Output:
Exception in thread "main" java.lang.IllegalArgumentException: -25 at java.base/java.util.stream.ReferencePipeline.skip(ReferencePipeline.java:476) at com.javaprogramto.java8.skip.SkipNegativeIndexExample.main(SkipNegativeIndexExample.java:13)
7. Conclusion
In this article, you've seen how to use the java 8 stream skip() method with examples.
No comments:
Post a Comment
Please do not add any spam links in the comments section.