Last Updated : 11 Jul, 2025
A
streamis a sequence of objects that supports various methods which can be pipelined to produce the desired result.
Slice of a Streammeans a stream of elements that exists in a specified limit, from the original stream.
Examples:Input: [11, 12, 13, 14, 15, 16, 17, 18, 19, 20] Output: [15, 16, 17, 18, 19] Explanation: The output contains a slice of the stream from index 4 to 8. Input: [1, 2, 3, 4, 5, 6, 7, 8, 9] Output: [2, 3, 4] Explanation: The output contains a slice of the stream from index 1 to 3.
Below are the methods to remove nulls from a List in Java:
// Java program to get slice of a stream using
// Stream skip() and limit()
import java.util.*;
import java.util.stream.Stream;
class GFG {
// Generic function to get Slice of a
// Stream from startIndex to endIndex
public static <T> Stream<T>
getSliceOfStream(Stream<T> stream, int startIndex,
int endIndex)
{
return stream
// specify the number of elements to skip
.skip(startIndex)
// specify the no. of elements the stream
// that should be limited
.limit(endIndex - startIndex + 1);
}
public static void main(String[] args)
{
// Create a new List with values 11 - 20
List<Integer> list = new ArrayList<>();
for (int i = 11; i <= 20; i++)
list.add(i);
// Create stream from list
Stream<Integer> intStream = list.stream();
// Print the stream
System.out.println("List: " + list);
// Get Slice of Stream
// containing of elements from the 4th index to 8th
Stream<Integer>
sliceOfIntStream = getSliceOfStream(intStream, 4, 8);
// Print the slice
System.out.println("\nSlice of Stream:");
sliceOfIntStream.forEach(System.out::println);
}
}
Output:
List: [11, 12, 13, 14, 15, 16, 17, 18, 19, 20] Slice of Stream: 15 16 17 18 19
// Java program to get slice of a stream using
// Collection skip() and limit()
import java.util.*;
import java.util.stream.*;
class GFG {
// Generic function to get Slice of a
// Stream from startIndex to endIndex
public static <T> Stream<T>
getSliceOfStream(Stream<T> stream, int startIndex, int endIndex)
{
return stream.collect(Collectors.collectingAndThen(
// 1st argument
// Convert the stream to list
Collectors.toList(),
// 2nd argument
list -> list.stream()
// specify the number of elements to skip
.skip(startIndex)
// specify the no. of elements the stream
// that should be limited
.limit(endIndex - startIndex + 1)));
}
public static void main(String[] args)
{
// Create a new List with values 11 - 20
List<Integer> list = new ArrayList<>();
for (int i = 11; i <= 20; i++)
list.add(i);
// Create stream from list
Stream<Integer> intStream = list.stream();
// Print the stream
System.out.println("List: " + list);
// Get Slice of Stream
// containing of elements from the 4th index to 8th
Stream<Integer>
sliceOfIntStream = getSliceOfStream(intStream, 4, 8);
// Print the slice
System.out.println("\nSlice of Stream:");
sliceOfIntStream.forEach(System.out::println);
}
}
Output:
List: [11, 12, 13, 14, 15, 16, 17, 18, 19, 20] Slice of Stream: 15 16 17 18 19
// Java program to get slice of a stream by
// fetching a sublist
import java.util.*;
import java.util.stream.*;
class GFG {
// Generic function to get Slice of a
// Stream from startIndex to endIndex
public static <T> Stream<T>
getSliceOfStream(Stream<T> stream, int startIndex, int endIndex)
{
return stream
// Convert the stream to list
.collect(Collectors.toList())
// Fetch the subList between the specified index
.subList(startIndex, endIndex + 1)
// Convert the subList to stream
.stream();
}
public static void main(String[] args)
{
// Create a new List with values 11 - 20
List<Integer> list = new ArrayList<>();
for (int i = 11; i <= 20; i++)
list.add(i);
// Create stream from list
Stream<Integer> intStream = list.stream();
// Print the stream
System.out.println("List: " + list);
// Get Slice of Stream
// containing of elements from the 4th index to 8th
Stream<Integer>
sliceOfIntStream = getSliceOfStream(intStream, 4, 8);
// Print the slice
System.out.println("\nSlice of Stream:");
sliceOfIntStream.forEach(System.out::println);
}
}
Output:
List: [11, 12, 13, 14, 15, 16, 17, 18, 19, 20] Slice of Stream: 15 16 17 18 19
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4