How to get Slice of a Stream in Java

11 Dec, 2018
A stream is a sequence of objects that supports various methods which can be pipelined to produce the desired result. Slice of a Stream means 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:
Using skip() and limit(): Stream API in Java provides skip() method which is used to discard the other non-required elements from the stream. It also provides limit() function which is applied to fetch the new stream with the specified index as limit, in the encountered order.Algorithm:Get the Stream to be sliced.Get the From and To index to be sliced from Stream as StartIndex and EndIndexCall skip() method to specify the number of elements to be skipped before the starting index as skip(startIndex)Call limit() method to specify the number of elements, from the stream, that should be limited as limit(endIndex – startIndex + 1)Return the Sliced Stream// 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
Using Collectors along with skip() and limit(): In this method, the Stream is converted to List and then a function of a collector to get sub-list of desired elements is used and the sub-list id converted back to a stream using stream.collect(Collectors.collectingAndThen()).Algorithm:Get the Stream to be sliced.Get the From and To index to be sliced from Stream as StartIndex and EndIndexUsing Collectors.collectingAndThen,Convert the Stream to List using Collectors.toList()Obtain the Stream from the List as list.stream()Call skip() method to specify the number of elements to be skipped before the starting index as skip(startIndex)Call limit() method to specify the number of elements, from the stream, that should be limited as limit(endIndex – startIndex + 1)Collect the sliced list stream using stream.collect()Return the Sliced Stream// 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
Fetching a SubList: This method involves converting a Stream into a List. Now this list is used to fetch a required subList from it between the specified index. And finally, this subList is converted back to Stream.Algorithm:Get the Stream to be sliced.Get the From and To index to be sliced from Stream as StartIndex and EndIndexConvert the Stream to List using Collectors.toList() and then collect it using stream.collect()Fetch the subList from the collected List with the startIndex and endIndex+1 as the limit using subList(startIndex, endIndex + 1)Convert the subList back to stream using stream()Return the Sliced Stream// Java program to get slice of a stream by// fetching a sublistimport 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

Using skip() and limit(): Stream API in Java provides skip() method which is used to discard the other non-required elements from the stream. It also provides limit() function which is applied to fetch the new stream with the specified index as limit, in the encountered order.Algorithm:Get the Stream to be sliced.Get the From and To index to be sliced from Stream as StartIndex and EndIndexCall skip() method to specify the number of elements to be skipped before the starting index as skip(startIndex)Call limit() method to specify the number of elements, from the stream, that should be limited as limit(endIndex – startIndex + 1)Return the Sliced Stream// 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

Algorithm:
Get the Stream to be sliced.Get the From and To index to be sliced from Stream as StartIndex and EndIndexCall skip() method to specify the number of elements to be skipped before the starting index as skip(startIndex)Call limit() method to specify the number of elements, from the stream, that should be limited as limit(endIndex – startIndex + 1)Return the Sliced Stream
Get the Stream to be sliced.
Get the From and To index to be sliced from Stream as StartIndex and EndIndex
Call skip() method to specify the number of elements to be skipped before the starting index as skip(startIndex)
Call limit() method to specify the number of elements, from the stream, that should be limited as limit(endIndex – startIndex + 1)
Return the Sliced Stream
// 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);    }}
List: [11, 12, 13, 14, 15, 16, 17, 18, 19, 20]

Slice of Stream:
15
16
17
18
19

Using Collectors along with skip() and limit(): In this method, the Stream is converted to List and then a function of a collector to get sub-list of desired elements is used and the sub-list id converted back to a stream using stream.collect(Collectors.collectingAndThen()).Algorithm:Get the Stream to be sliced.Get the From and To index to be sliced from Stream as StartIndex and EndIndexUsing Collectors.collectingAndThen,Convert the Stream to List using Collectors.toList()Obtain the Stream from the List as list.stream()Call skip() method to specify the number of elements to be skipped before the starting index as skip(startIndex)Call limit() method to specify the number of elements, from the stream, that should be limited as limit(endIndex – startIndex + 1)Collect the sliced list stream using stream.collect()Return the Sliced Stream// 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

Algorithm:
Get the Stream to be sliced.Get the From and To index to be sliced from Stream as StartIndex and EndIndexUsing Collectors.collectingAndThen,Convert the Stream to List using Collectors.toList()Obtain the Stream from the List as list.stream()Call skip() method to specify the number of elements to be skipped before the starting index as skip(startIndex)Call limit() method to specify the number of elements, from the stream, that should be limited as limit(endIndex – startIndex + 1)Collect the sliced list stream using stream.collect()Return the Sliced Stream
Get the Stream to be sliced.
Get the From and To index to be sliced from Stream as StartIndex and EndIndex
Using Collectors.collectingAndThen,
Convert the Stream to List using Collectors.toList()
Obtain the Stream from the List as list.stream()
Call skip() method to specify the number of elements to be skipped before the starting index as skip(startIndex)
Call limit() method to specify the number of elements, from the stream, that should be limited as limit(endIndex – startIndex + 1)
Collect the sliced list stream using stream.collect()
Return the Sliced Stream
// 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);    }}
List: [11, 12, 13, 14, 15, 16, 17, 18, 19, 20]

Slice of Stream:
15
16
17
18
19

Fetching a SubList: This method involves converting a Stream into a List. Now this list is used to fetch a required subList from it between the specified index. And finally, this subList is converted back to Stream.Algorithm:Get the Stream to be sliced.Get the From and To index to be sliced from Stream as StartIndex and EndIndexConvert the Stream to List using Collectors.toList() and then collect it using stream.collect()Fetch the subList from the collected List with the startIndex and endIndex+1 as the limit using subList(startIndex, endIndex + 1)Convert the subList back to stream using stream()Return the Sliced Stream// Java program to get slice of a stream by// fetching a sublistimport 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

Algorithm:
Get the Stream to be sliced.Get the From and To index to be sliced from Stream as StartIndex and EndIndexConvert the Stream to List using Collectors.toList() and then collect it using stream.collect()Fetch the subList from the collected List with the startIndex and endIndex+1 as the limit using subList(startIndex, endIndex + 1)Convert the subList back to stream using stream()Return the Sliced Stream
Get the Stream to be sliced.
Get the From and To index to be sliced from Stream as StartIndex and EndIndex
Convert the Stream to List using Collectors.toList() and then collect it using stream.collect()
Fetch the subList from the collected List with the startIndex and endIndex+1 as the limit using subList(startIndex, endIndex + 1)
Convert the subList back to stream using stream()
Return the Sliced Stream
// Java program to get slice of a stream by// fetching a sublistimport 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);    }}
List: [11, 12, 13, 14, 15, 16, 17, 18, 19, 20]

Slice of Stream:
15
16
17
18
19

Java-Collections
java-stream
Java-Stream-programs
Java
Java Programs
Java
Java-Collections
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Object Oriented Programming (OOPs) Concept in Java
How to iterate any Map in Java
Interfaces in Java
HashMap in Java with Examples
ArrayList in Java
Initializing a List in Java
Java Programming Examples
Convert a String to Character Array in Java
Convert Double to Integer in Java
Implementing a Linked List in Java using Class