
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
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
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
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]
- Related Articles
- How to remove duplicates from an ArrayList in Java?
- How to remove an element from ArrayList in Java?
- How to get Sublist of an ArrayList using Java?
- How to remove an element from ArrayList or, LinkedList in Java?
- How to remove element from ArrayList in Java?
- Remove duplicate items from an ArrayList in Java
- Java Program to Remove a Sublist from a List
- How to remove the redundant elements from an ArrayList object in java?
- Java Program to Remove Repeated Element from An ArrayList
- How to remove an item from an ArrayList in C#?
- How to remove an item from an ArrayList in Kotlin?
- Remove an element from an ArrayList using the ListIterator in Java
- Java Program to Remove duplicate elements from ArrayList
- How to create an ArrayList from an Array in Java?
- Remove all elements from the ArrayList in Java
