How to remove an element from ArrayList or, LinkedList in Java?

JavaObject Oriented ProgrammingProgramming

The ArrayList and, LinkedList classes implements the List interface of the java.util package. This interface provided two variants of the remove() method to remove particular elements as shown below −

  • E remove(int index)

  • boolean remove(Object o) −

Using one of these methods you can delete a desired element from the List or, linkedList in Java.

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. 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 1, an IndexOutOfBoundsException exception is raised.

Example

 Live Demo

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));
   }
}

Output

Contents of the Array List: [JavaFx, Java, WebGL, OpenCV]
Elements removed:
JavaFX
OpenCV

Contents of the linked List: [Java, WebGL]
Elements removed:
Krishna
Radha

boolean remove(Object o) − 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

 Live Demo

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);
   }
}

Output

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]

The remove() method of the Iterator object

In addition to these two method you can also remove elements of the LinkedList or ArrayList objects using the remove() of the Iterator class.

Example

 Live Demo

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());
      }
   }
}

Output

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
raja
Published on 14-Oct-2019 12:14:10
Advertisements