How to remove a SubList from an ArrayList in Java?


Using the subList() and the clear() methods

The subList() method of the List interface accepts two integer values representing indexes of the elements and, returns a view of the of the current List object removing the elements between the specified indices.

The clear() method of List interface removes all the elements from the current List object.

Therefore, to remove a specific sub list of an array list you just need to call these two methods on your list object by specifying the boundaries of the sub list you need to remove as −

obj.subList().clear();

Example

 Live Demo

import java.util.ArrayList;
public class RemovingSubList {
   public static void main(String[] args){
      //Instantiating an ArrayList object
      ArrayList<String> list = new ArrayList<String>();
      list.add("JavaFX");
      list.add("Java");
      list.add("WebGL");
      list.add("OpenCV");
      list.add("OpenNLP");
      list.add("JOGL");
      list.add("Hadoop");
      list.add("HBase");
      list.add("Flume");
      list.add("Mahout");
      list.add("Impala");
      System.out.println("Contents of the Array List: \n"+list);
      //Removing the sub list
      list.subList(4, 9).clear();
      System.out.println("Contents of the Array List after removing the sub list: \n"+list);
   }
}

Output

Contents of the Array List:
[JavaFX, Java, WebGL, OpenCV, OpenNLP, JOGL, Hadoop, HBase, Flume, Mahout, Impala]
Contents of the Array List after removing the sub list:
[JavaFX, Java, WebGL, OpenCV, Mahout, Impala]

Using the removeRange method()

The removeRange() method of the AbstractList class method accepts two integer values representing indexes of the elements of the current ArrayList and removes them.

But this is a protected method and to use this you need to

  • Inherit the ArrayList class (from your class) using the extends keyword.

  • Instantiate your class.

  • Add elements to the obtained object.

  • Then, remove the desired subList using the removeRange() method.

Example

 Live Demo

import java.util.ArrayList;
public class RemovingSubList extends ArrayList<String>{
   public static void main(String[] args){
      RemovingSubList list = new RemovingSubList();
      //Instantiating an ArrayList object
      list.add("JavaFX");
      list.add("Java");
      list.add("WebGL");
      list.add("OpenCV");
      list.add("OpenNLP");
      list.add("JOGL");
      list.add("Hadoop");
      list.add("HBase");
      list.add("Flume");
      list.add("Mahout");
      list.add("Impala");
      System.out.println("Contents of the Array List: \n"+list);
      //Removing the sub list
      list.removeRange(4, 9);
      System.out.println("Contents of the Array List after removing the sub list: \n"+list);
   }
}

Output

Contents of the Array List:
[JavaFX, Java, WebGL, OpenCV, OpenNLP, JOGL, Hadoop, HBase, Flume, Mahout, Impala]
Contents of the Array List after removing the sub list:
[JavaFX, Java, WebGL, OpenCV, Mahout, Impala]
raja
Published on 11-Oct-2019 10:47:22
Advertisements