In this tutorial, we will see “What is a Skip() method in Java 8 Stream and how we can use it?” skip stream java 8
Skip() method in Java Stream...!!! Share on X
Streams SKIP() Method
Syntax & Description
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.
This is a stateful intermediate operation.
Syntax: Stream<T> skip(long n)
How to use it – an Example?
/**
* Using Skip method as name suggest, Skip first 5 integers and print the rest
* @author Deepak Verma
*
*/
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
public class Skip_Java8Stream_Example {
public static void main(String args[]) {
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
// Get a stream of the numbers
Stream<Integer> stream = numbers.stream();
// Skip the first 5 elements and print the remaining elements
stream.skip(5)
.forEach(System.out::println);
}
}
Output:
6 7 8 9 10
stream to array java
skip method java 8 stream
Java 8 – Stream Skip() method with Example Share on X
Do you like this Post? – then check my other helpful posts:
- Convert a Stream to a List in Java 8
- Stream maptoint in Java 8 with examples
- Double the numbers of specified ArrayList using Streams
- Double the even / odd numbers of a specified ArrayList using Streams
- How to check if Number is Prime or not using Streams
- Retrieve Even/Odd Numbers within the Range using Java 8 Streams
- How to add/sum up the ArrayList integers using Java8 Streams
- Generate Prime Numbers in Java 8 using Streams
- Comparator example – Collections sort with/without Lambda in Java 8
- How to pass function as a parameter in a method in Java 8?
- Remove duplicates from ArrayList in Java 8
- ForEach examples for Map/List in Java 8
Other Useful References: