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
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:
2 3 4 5
2. Using List.subList method
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:
[2, 3, 4, 5]
3. Using Plain / Traditional approach
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:
[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