How to get Slice of a Stream in Java
An object sequence that allows for various methods to be pipelined to achieve a particular outcome is called a stream. A stream of elements that remain inside a given bound from the original stream is referred to as a slice of a stream.
Examples 1:
Input:
int list = [15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]
Output:
The slice of a stream is [18, 19, 20, 21, 22, 23, 24]
Explanation:
The result for the specified list includes the elements of the stream from index 3 to index 9.
Example 2:
Input:
int list = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
Output:
The slice of a stream is [8, 9, 10, 11, 12]
Explanation:
The result for the specified list includes an element of the stream from index 6 to index 10.
Approach: Using skip() and limit()
The skip() function in the Java Stream API is used to remove any other unneeded elements from the stream. It also offers the limit() function, which is used to retrieve a new stream in the same order and with the given index as the limit.
Algorithm:
Step 1: Get the stream ready for slicing.
Step 2: Retrieve the StartIndex and EndIndex from the stream, together with the From and To index.
Step 3: Use the skip() method to tell it how many elements need to be skipped before the starting index, like this: skip(startIndex).
Step 4: Call the limit() function to indicate the number of stream elements that have to be restricted, represented as limit(endIndex – startIndex + 1)
Step 5: Return the Sliced Stream
Implementation:
FileName: SkipandLimit.java
import java.io.*; import java.util.*; import java.util.stream.Stream; public class SkipandLimit { // Generic method to obtain a stream's slice // from startIndex to endTable of contents public staticStream getSliceOfStream(Stream stream, int start, int end) { return stream // indicate the total number of elements to be skipped .skip(start) // Indicate the number of elements in the // stream that needs to be restricted. .limit(end - start + 1); } public static void main(String[] args) { // Make a new list with the numbers 15 to 25. List l = new ArrayList<>(); for (int i = 15; i <= 25; i++) l.add(i); // Creating the stream from the given list Stream ListStream = l.stream(); // displaying the stream System.out.println("The given List is: " + l); // Obtain a Slice of the Stream with Elements // from the Third to the Ninth Index Stream sliceOfIntStream = getSliceOfStream(ListStream, 3, 9); // displaying the slice System.out.println(" The Slice of the Stream for a list:"); sliceOfIntStream.forEach(System.out::println); } }
Output:
The given List is: [15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25] The Slice of the Stream for a list: 18 19 20 21 22 23 24
Approach: Using Collectors along with skip() and limit()
This approach involves first converting the stream to a list and then using a collector's function to obtain a sub-list containing the desired elements. The sub-list is subsequently transformed back to a stream using stream.collect(Collectors.collectingAndThen()).
Algorithm:
Step 1: Set the stream for slicing.
Step 2: Retrieve the From and To indexes (StartIndex and EndIndex) from the stream.
Step 3: Using Collectors.assembling and following the below steps.
Step 4: Use Collectors.toList() to convert the stream to a list.
Step 5: Retrieve the stream as a list from the list.stream()
Step 6: Call the skip() method to provide in a skip(startIndex) the number of elements to be skipped before the starting index.
Step 7: Call the limit() method to define limit(endIndex – startIndex + 1), which is the number of entries from the stream that should be limited.
Step 8: Set up a stream to gather the sliced list stream.gather()
Step 9: Return the Sliced Stream back
Implementation:
FileName: CollectorsSkipandLimit.java
import java.io.*; import java.util.*; import java.util.stream.*; public class CollectorsSkipandLimit { // Generic method to obtain a stream's slice // from startIndex to endTable of contents public staticStream getSliceOfStream(Stream stream, int start, int end) { return stream.collect(Collectors.collectingAndThen( // Initial argument Transform the stream into a list. Collectors.toList(), // 2nd argument l -> l.stream() // indicate the total number of items to be skipped .skip(start) // Indicate the number of elements in the // stream that needs to be restricted. .limit(end - start + 1))); } public static void main(String[] args) { // Creating the new List with values 15 - 25 List l = new ArrayList<>(); for (int i = 15; i <= 25; i++) l.add(i); // Creating the stream from the given list Stream listStream = l.stream(); // displaying the stream System.out.println("The given List is: " + l); // Obtain a Slice of the Stream with Elements // from the Third to the Ninth Index Stream sliceOfIntStream = getSliceOfStream(listStream, 3, 9); // displaying the slice System.out.println(" The Slice of a Stream is:"); sliceOfIntStream.forEach(System.out::println); } }
Output:
The given List is: [15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25] The Slice of a Stream is: 18 19 20 21 22 23 24
Approach: Fetching a SubList
This technique involves converting a stream into a list. This list is now utilized to retrieve a necessary subList between the given index. In conclusion, this subList is changed back to a Stream.
Algorithm:
Step1: Set the stream for slicing.
Step 2: Retrieve the From and To indexes to be divided into StartIndex and EndIndex from the stream.
Step 3: Apply collectors to convert the stream to a list.toList() before utilizing stream to gather it.gathering()
Step 4: Implementing subList(startIndex, endIndex + 1), retrieve the subList from the gathered List using startIndex and endIndex+1 as the limit.
Step 5: Call stream() to return the subList to a stream.
Step 6: Return the Sliced Stream back.
Implementation:
FileName: FetchingSubList.java
import java.io.*; import java.util.*; import java.util.stream.*; public class FetchingSubList { // Generic method to obtain a stream's slice // from startIndex to endTable of contents public staticStream getSliceOfStream(Stream stream, int start, int end) { return stream // Converting from the stream to a list .collect(Collectors.toList()) // Retrieve the subList in between the given index. .subList(start, end + 1) // The sub list should be streamed. .stream(); } public static void main(String[] args) { // Construct a new list with the values 15 to 25. List l = new ArrayList<>(); for (int i = 15; i <= 25; i++) l.add(i); // Creating the stream from a list Stream listStream = l.stream(); // displaying the stream System.out.println("The given List is: " + l); // Get a slice of the stream that contains elements // ranging from the 3rd index to the 9th Stream sliceOfIntStream = getSliceOfStream(listStream, 3, 9); // displaying the slice System.out.println(" The Slice of a Stream is:"); sliceOfIntStream.forEach(System.out::println); } }
Output:
The given List is: [15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25] The Slice of a Stream is: 18 19 20 21 22 23 24