Get a slice from a List in Java
This post will discuss how to get a slice (or a sub-list) from a List in Java.
1. Using list.subList()
method
You can use the List.sublist()
method to get a view of the portion of this list between the specified range of indices.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import java.util.List; import java.util.stream.Collectors; import java.util.stream.IntStream; class Main { public static void main(String[] args) { List<Character> chars = IntStream.rangeClosed(65, 90) .mapToObj(x -> (char)x) .collect(Collectors.toList()); int startIndex = 3; int endIndex = 5; List<Character> sublist = chars.subList(startIndex, endIndex + 1); System.out.println(sublist); } } |
Output:
[D, E, F]
Note that the sublist returned by the subList()
method is backed by the original list, any non-structural changes in the sublist are reflected in the original list and vice-versa. If you want to avoid this behavior, it is advisable to wrap the list using the ArrayList
constructor. For instance,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; import java.util.stream.IntStream; class Main { public static void main(String[] args) { List<Character> chars = IntStream.rangeClosed(65, 90) .mapToObj(x -> (char)x) .collect(Collectors.toList()); List<Character> sublist = new ArrayList<>(chars.subList(5, 6)); System.out.println(sublist); } } |
Output:
[D, E, F]
2. Using Stream API
The idea here is to get the stream of the elements in the list and call the skip(n)
method to skip the first n
elements. Then call the limit(k)
method to return a new stream consisting of the next k
elements in the encounter order and collect the stream to a new list.
This process generates the sublist of the original list in the range [n, n + k]
.
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 |
import java.util.List; import java.util.stream.Collectors; import java.util.stream.IntStream; class Main { public static<T> List<T> getSubList(List<T> list, int startIndex, int endIndex) { return list.stream() .skip(startIndex) .limit(endIndex - startIndex + 1) .collect(Collectors.toList()); } public static void main(String[] args) { List<Character> chars = IntStream.rangeClosed(65, 90) .mapToObj(x -> (char)x) .collect(Collectors.toList()); int startIndex = 3; int endIndex = 5; List<Character> sublist = getSubList(chars, startIndex, endIndex); System.out.println(sublist); } } |
Output:
[D, E, F]
3. Using Enhanced For loop
Here’s a version without streams using an enhanced for loop (self-explanatory):
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 32 33 34 |
import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; import java.util.stream.IntStream; class Main { public static<T> List<T> getSubList(List<T> list, int startIndex, int endIndex) { List<T> sublist = new ArrayList<>(); long index = 0; for (T e : list) { if (index >= startIndex && index <= endIndex) { sublist.add(e); } else if (index > endIndex) { break; } index++; } return sublist; } public static void main(String[] args) { List<Character> chars = IntStream.rangeClosed(65, 90) .mapToObj(x -> (char)x) .collect(Collectors.toList()); int startIndex = 3; int endIndex = 5; List<Character> sublist = getSubList(chars, startIndex, endIndex); System.out.println(sublist); } } |
Output:
[D, E, F]
The code can be simplified using a regular for loop, as shown below:
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 |
import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; import java.util.stream.IntStream; class Main { public static<T> List<T> getSubList(List<T> list, int startIndex, int endIndex) { List<T> sublist = new ArrayList<>(); for (int i = startIndex; i <= Integer.min(endIndex, list.size()); i++) { sublist.add(list.get(i)); } return sublist; } public static void main(String[] args) { List<Character> chars = IntStream.rangeClosed(65, 90) .mapToObj(x -> (char)x) .collect(Collectors.toList()); int startIndex = 3; int endIndex = 5; List<Character> sublist = getSubList(chars, startIndex, endIndex); System.out.println(sublist); } } |
Output:
[D, E, F]
That’s all about getting a slice from a List in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)