In this post, we will see “How to get Sublist from an ArrayList in Java 8?” we are going to see how to achieve it using Streams API and will see some traditional methods too.
Get Sublist From an ArrayList using Java 8 Streams..!!! Share on X
Here, we are going to cover below points:
- Using Streams
- Using List.subList method
- Using Plain / Traditional approach
Let’s begin,
1. Using Streams
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 | import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; public class Sublist_From_List_Java8_Example { public static void main(String[] args) { // Create the original list with integer elements List <Integer> originalList = Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9); // Get the substream of the specified range of elements and before storing them into the subList, convert the substream back to list first List <Integer> subList = subStream(originalList.stream(), 2, 5).collect(Collectors.toList()); // Print the sublist subList.forEach(System.out::println); } // Covert the list into stream and apply the sublist method before returning it back to main method, convert it back to stream. public static Stream <Integer> subStream(Stream <Integer> stream, int startIndex, int endIndex) { return stream .collect(Collectors.toList()) .subList(startIndex, endIndex + 1) .stream(); } } |
Output:
1 2 3 4 | 2 3 4 5 |
2. Using List.subList method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | import java.util.Arrays; import java.util.List; public class Sublist_From_List_Java_Example { public static void main(String[] args) { // Create the original list with integer elements List < Integer > originalList = Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9); // Extract the specified range of elements from the original list and store them into the sublist List < Integer > subList = originalList.subList(2, 6); // Print the sublist System.out.println(subList); } } |
Output:
1 | [2, 3, 4, 5] |
3. Using Plain / Traditional approach
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 31 | import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class Sublist_From_List_Plain_Approach_Java_Example { public static void main(String[] args) { // Create the original list with integer elements List < Integer > originalList = Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9); // Print the Sublist System.out.println(sublist(originalList, 2, 5)); } //Regular function to get sublist between two indexes from a List public static List < Integer > sublist(List < Integer > originalList, int startIndex, int endIndex) { // Create a blank Sublist List < Integer > subList = new ArrayList < > (); // Add the elements between the specified range from the original list into the sublist for (int i = startIndex; i <= endIndex; i++) { subList.add(originalList.get(i)); } // Return the Sublist to the main method return subList; } } |
Output:
1 | [2, 3, 4, 5] |
All the above ways will generate the same output.
Get Sublist From an ArrayList using Java 8 Streams..!!! Share on X
Do you like this Post? – then check my other helpful posts:
- Double the even / odd numbers of a specified ArrayList using Streams
- Double the numbers of specified ArrayList using Streams