Get a slice of a Stream in Java
This post will discuss how to get a slice of a stream in Java. In other words, we need to get a new stream of elements between two specified positions.
1. Skip elements
The idea is to call the skip(n) method to discard the first n elements of the specified stream and then use the limit(k) method to return a new stream consisting of the next k elements in the encounter order.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
import java.util.ArrayList; import java.util.List; import java.util.stream.Stream; class Main { // Generic method to get a slice of a stream from `fromIndex` to `toIndex` public static<T> Stream<T> getSlice(Stream<T> stream, int fromIndex, int toIndex) { return stream // specify the total number of elements to skip .skip(fromIndex) // specify the total number of elements the stream should be limited to .limit(toIndex - fromIndex + 1); } public static void main(String[] args) { List<Integer> collection = new ArrayList<>(); for (int i = 0; i < 10; i++) { collection.add(i); } Stream<Integer> stream = collection.stream(); // Get a stream containing elements from the 2nd index to the 5th index Stream<Integer> slice = getSlice(stream, 2, 5); slice.forEach(System.out::println); } } |
Please note that:
- Both
skip()andlimit()offer poor performance with parallel streams since consistency with the encounter order is required. - The
limit(fromIndex, toIndex)throws ajava.lang.IllegalArgumentExceptioniffromIndexis more thantoIndex. - If
toIndexis more than the total number of elements in the stream, then a stream ranging fromfromIndextill the last index is returned. - If the stream has fewer elements than
fromIndex, an empty stream is returned. - If
fromIndexis negative,skip(fromIndex)throws ajava.lang.IndexOutOfBoundsException.
2. Get a Sublist
Here, the idea is to convert the stream to a list, use the sublist() method to get the sublist of elements between specified indexes, and finally convert it back to a stream.
|
1 2 3 4 5 6 7 8 |
// Generic method to get a slice of a stream from `fromIndex` to `toIndex` public static<T> Stream<T> getSlice(Stream<T> stream, int fromIndex, int toIndex) { return stream .collect(Collectors.toList()) .subList(fromIndex, toIndex + 1) .stream(); } |
This method also throws java.lang.IndexOutOfBoundsException if fromIndex is negative or toIndex is more than the stream’s size.
3. Using Collectors
This is similar to the first approach but uses collectors to first convert the stream to a list and then use a finisher method of a collector to get a sublist of desired elements and convert the sublist back to a stream.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
// Generic method to get a slice of a stream from `fromIndex` to `toIndex` public static<T> Stream<T> getSlice(Stream<T> stream, int fromIndex, int toIndex) { return stream.collect(Collectors.collectingAndThen ( Collectors.toList(), list -> list.stream() .skip(fromIndex) .limit(toIndex - fromIndex + 1) ) ); } |
That’s all about getting a slice of a Stream in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)