
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Remove Element from ArrayList or LinkedList in Java
The ArrayList and LinkedList classes implement the List interface of the java.util package. This interface provided two variants of the remove() method to remove particular elements, as shown below -
-
Using remove(int index)
-
Using remove(Object o)
There are two additional removal approaches provided by the Collection and Iterator interfaces. They are -
-
Using iterator.remove()
-
Using removeIf()
Using one of these methods, you can delete a desired element from the List or list in Java.
Using E remove(int index)
This method accepts an integer representing a particular position in the List object and removes the element at the given position. Here, E represents the type of element being removed(like String, integer, etc). If the remove operation is successful, this method returns the element that has been removed.
If the index value passed to this method is less than 0 or greater than or equal to the size of the list, an IndexOutOfBoundsException exception is raised.
Example
This program shows how to remove elements from an ArrayList using the remove(int index) method. It also shows that the same method works similarly with a LinkedList when removing elements by position.
import java.util.ArrayList; public class RemoveExample { public static void main(String[] args) { //Instantiating an ArrayList object ArrayList <String> arrayList = new ArrayList <String> (); arrayList.add("JavaFX"); arrayList.add("Java"); arrayList.add("WebGL"); arrayList.add("OpenCV"); System.out.println("Contents of the Array List: " + arrayList); //Removing elements System.out.println("Elements removed: "); System.out.println(arrayList.remove(0)); System.out.println(arrayList.remove(2)); System.out.println(" "); //Instantiating an LinkedList object ArrayList < String > linkedList = new ArrayList <String> (); linkedList.add("Krishna"); linkedList.add("Satish"); linkedList.add("Mohan"); linkedList.add("Radha"); System.out.println("Contents of the linked List: " + arrayList); //Removing elements System.out.println("Elements removed: "); System.out.println(linkedList.remove(0)); System.out.println(linkedList.remove(2)); } }
Let us compile and run the above program, which will give the following result -
Contents of the Array List: [JavaFx, Java, WebGL, OpenCV] Elements removed: JavaFX OpenCV Contents of the linked List: [Java, WebGL] Elements removed: Krishna Radha
Using remove(Object o) Method
This method accepts an object representing an element in the List and removes the first occurrence of the given element. This method returns a boolean value, which is -
-
true, if the operation is successful.
-
false, if the operation is unsuccessful.
Example
This program explains how to remove elements from an ArrayList using the remove(Object o) method. It removes elements by their value (not position) and works the same way for both ArrayList and LinkedList.
import java.util.ArrayList; public class RemoveExample { public static void main(String[] args) { //Instantiating an ArrayList object ArrayList < String > arrayList = new ArrayList < String >(); arrayList.add("JavaFX"); arrayList.add("Java"); arrayList.add("WebGL"); arrayList.add("OpenCV"); System.out.println("Contents of the Array List: " + arrayList); //Removing elements System.out.println("Elements removed: "); System.out.println(arrayList.remove("JavaFX")); System.out.println(arrayList.remove("WebGL")); System.out.println("Contents of the array List after removing elements: " + arrayList); System.out.println(" "); //Instantiating an LinkedList object ArrayList < String > linkedList = new ArrayList <String >(); linkedList.add("Krishna"); linkedList.add("Satish"); linkedList.add("Mohan"); linkedList.add("Radha"); System.out.println("Contents of the linked List: " + linkedList); //Removing elements System.out.println("Elements removed: "); System.out.println(linkedList.remove("Satish")); System.out.println(linkedList.remove("Mohan")); System.out.println("Contents of the linked List after removing elements: " + linkedList); } }
Let us compile and run the above program, which will give the following result -
Contents of the Array List: [JavaFX, Java, WebGL, OpenCV] Elements removed: true true Contents of the array List after removing elements: [Java, OpenCV] Contents of the linked List: [Krishna, Satish, Mohan, Radha] Elements removed: true true Contents of the linked List after removing elements: [Krishna, Radha]
Using Iterator.remove() Method
In addition to these two methods, you can also remove elements of the LinkedList or ArrayList objects using the remove() method of the Iterator class. If you are looping through a list and want to remove elements from the list, you can use an iterator.
Example
This program shows how to remove elements from an ArrayList using an Iterator. The remove() method of Iterator deletes the last element returned by next() while looping through the list.
import java.util.ArrayList; import java.util.Iterator; public class RemoveExample { public static void main(String[] args) { //Instantiating an ArrayList object ArrayList <String> arrayList = new ArrayList <String >(); arrayList.add("JavaFX"); arrayList.add("Java"); arrayList.add("WebGL"); arrayList.add("OpenCV"); System.out.println("Contents of the Array List: " + arrayList); //Retrieving the Iterator object Iterator < String > it1 = arrayList.iterator(); it1.next(); it1.remove(); System.out.println("Contents of the array List after removing elements: "); while (it1.hasNext()) { System.out.println(it1.next()); } //Instantiating an LinkedList object ArrayList <String> linkedList = new ArrayList <String >(); linkedList.add("Krishna"); linkedList.add("Satish"); linkedList.add("Mohan"); linkedList.add("Radha"); System.out.println("Contents of the linked List: " + linkedList); //Retrieving the Iterator object Iterator <String> it2 = linkedList.iterator(); it2.next(); it2.remove(); System.out.println("Contents of the linked List after removing elements: "); while (it2.hasNext()) { System.out.println(it2.next()); } } }
Following is the output of the above program -
Contents of the Array List: [JavaFX, Java, WebGL, OpenCV] Contents of the array List after removing elements: Java WebGL OpenCV Contents of the linked List: [Krishna, Satish, Mohan, Radha] Contents of the linked List after removing elements: Satish Mohan Radha
Using removeIf() Method
The removeIf() method is used to remove all elements from a list that match a given Condition.
import java.util.ArrayList; import java.util.LinkedList; public class RemoveIfBothExample { public static void main(String[] args) { // ArrayList example ArrayList < String > arrayList = new ArrayList < >(); arrayList.add("Alice"); arrayList.add("Bob"); arrayList.add("Andrew"); arrayList.add("Charlie"); System.out.println("ArrayList before removal: " + arrayList); arrayList.removeIf(name -> name.startsWith("A")); System.out.println("ArrayList after removal: " + arrayList); // LinkedList example LinkedList < String > linkedList = new LinkedList < >(); linkedList.add("Ankit"); linkedList.add("Brian"); linkedList.add("Amy"); linkedList.add("David"); System.out.println("\nLinkedList before removal: " + linkedList); linkedList.removeIf(name -> name.startsWith("A")); System.out.println("LinkedList after removal: " + linkedList); } }
Let us compile and run the above program, which will give the following result -
ArrayList before removal: [Alice, Bob, Andrew, Charlie] ArrayList after removal: [Bob, Charlie] LinkedList before removal: [Ankit, Brian, Amy, David] LinkedList after removal: [Brian, David]