Remove a sublist from an ArrayList in Java
This post will discuss how to remove a sublist from an ArrayList in Java. The solution removes the specific range of elements from a list.
There are several ways to remove a sublist from an ArrayList in Java, depending on whether we want to use built-in methods or write our own logic. Here are some of the most common ways to remove a sublist from an ArrayList in Java:
1. Using List.clear() method
This is the recommended approach to remove a range of elements from a list. The idea is to get a view of the specified range within the list using the subList() method and call the clear() method on it. The returned sublist is backed by the list, and any non-structural changes on it are reflected in the original list. Note that the clear() method does not change the size of the list, but simply sets the underlying array elements to null.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.util.ArrayList; import java.util.Arrays; import java.util.List; class Main { public static void main(String[] args) { List<Integer> list = new ArrayList<>(Arrays.asList(2, 1, 4, 3, 7, 5, 9)); int from = 2; int to = 4; list.subList(from, to + 1).clear(); // remove [4, 3, 7] System.out.println(list); // [2, 1, 5, 9] } } |
2. Using List.removeIf() method
Another option to remove a sublist from an ArrayList in Java using the List.removeIf() method. We need to define a predicate that sets the condition for removing the elements from the list, and then call the removeIf() method on the list with predicate as an argument. This method will iterate over the list and remove all the elements that satisfy the predicate. This modifies the original list and does not create a new one. It internally uses the Iterator’s remove() method to avoid java.util.ConcurrentModificationException. Here is an example of how to use the List.removeIf() method to remove a sublist from an ArrayList in Java:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.util.ArrayList; import java.util.Arrays; import java.util.List; class Main { public static void main(String[] args) { List<Integer> list = new ArrayList<>(Arrays.asList(2, 1, 4, 3, 7, 5, 9)); int from = 2; int to = 4; list.subList(from, to + 1).removeIf(x -> true); // remove [4, 3, 7] System.out.println(list); // [2, 1, 5, 9] } } |
3. Using a loop
We can also write our own method to remove a sublist from an ArrayList by using a for loop and the List.remove(int) method that removes the element at the specified position. We can use this method as follows:
|
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.Arrays; import java.util.List; class Main { public static void removeRange(List<Integer> list, int fromIndex, int toIndex) { // check if the indices are valid if (fromIndex < 0 || toIndex > list.size() || fromIndex > toIndex) { throw new IndexOutOfBoundsException(); } // loop through the sublist and remove each element for (int i = fromIndex; i <= toIndex; i++) { // remove the element at index i list.remove(fromIndex); } } public static void main(String[] args) { List<Integer> list = new ArrayList<>(Arrays.asList(2, 1, 4, 3, 7, 5, 9)); int from = 2; int to = 4; removeRange(list, from, to); // remove [4, 3, 7] System.out.println(list); // [2, 1, 5, 9] } } |
That’s all about removing a sublist from an ArrayList 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 :)