How to remove a SubList from a List in Java
Given a list in Java, the task is to remove all the elements in the sublist whose index is between fromIndex, inclusive, and toIndex, exclusive. The range of the index is defined by the user.
Example:
Input list = [1, 2, 3, 4, 5, 6, 7, 8], fromIndex = 2, endIndex = 4
Output [1, 2, 5, 6, 7, 8]
Input list = [‘G’, ‘E’, ‘E’, ‘G’, ‘G’, ‘K’, ‘S’], fromIndex = 3, endIndex = 5
Output [‘G’, ‘E’, ‘E’, ‘K’, ‘S’]
-
Method 1: Using subList() and clear() method
Syntax:
List.subList(int fromIndex, int toIndex).clear()
Example:
// Java code to remove a subList using// subList(a, b).clear() methodimportjava.util.*;publicclassAbstractListDemo {publicstaticvoidmain(String args[]){// Creating an empty AbstractListAbstractList<String>list =newLinkedList<String>();// Using add() method// to add elements in the listlist.add("GFG");list.add("for");list.add("Geeks");list.add("computer");list.add("portal");// Output the listSystem.out.println("Original List: "+ list);// subList and clear method// to remove elements// specified in the rangelist.subList(1,3).clear();// Print the final listSystem.out.println("Final List: "+ list);}}chevron_rightfilter_noneOutput:Original List: [GFG, for, Geeks, computer, portal] Final List: [GFG, computer, portal]
Note: Classes which can inherit AbstractList:
-
Method 2: Using removeRange() method
Syntax:
List.removeRange(int fromIndex, int toIndex)
Example:
// Java code to remove a subList// using removeRange() methodimportjava.util.*;// since removeRange() is a protected method// ArrayList has to be extend the classpublicclassGFGextendsArrayList<Integer> {publicstaticvoidmain(String[] args){// create an empty array listGFG arr =newGFG();// use add() method// to add values in the listarr.add(1);arr.add(2);arr.add(3);arr.add(4);arr.add(5);arr.add(6);arr.add(7);arr.add(8);// prints the list before removingSystem.out.println("Original List: "+ arr);// removing elements in the list// from index 2 to 4arr.removeRange(2,4);System.out.println("Final List: "+ arr);}}chevron_rightfilter_noneOutput:Original List: [1, 2, 3, 4, 5, 6, 7, 8] Final List: [1, 2, 5, 6, 7, 8]
Recommended Posts:
- List sublist() Method in Java with Examples
- Remove elements from a List that satisfy given predicate in Java
- List remove(int index) method in Java with Examples
- Java program to remove nulls from a List Container
- List remove(Object obj) method in Java with Examples
- AbstractSequentialList subList() method in Java with Example
- Stack subList() method in Java with Example
- Vector subList() Method in Java
- CopyOnWriteArrayList subList() method in Java with Examples
- AbstractList subList() method in Java with Examples
- ArrayList subList() method in Java with Examples
- How to remove indentation from an unordered list item using CSS?
- Program to convert List of String to List of Integer in Java
- Program to convert List of Integer to List of String in Java
- ConcurrentSkipListSet remove() method in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



