How to remove element from arraylist in java

In this post we are going to learn how to remove element from arraylist in java.

How to remove element from arraylist in java

how to remove element from arraylist in java

To remove element from ArrayList, remove() method is used. This method removes the element at the specified position in this list.

Shifts any subsequent elements to the left (subtracts one from their indices). Returns the element that was removed from the list.

Remove method has two overloaded methods,

  • remove(int index) – removes first specified element from list.
  • remove(Object o) – removes element at specified position in given list.

Remove (int index) method accepts index to be removed. As you can see the output, the passed parameter is considered as index.

Let’s see java program on ArrayList remove(int index) method example,

import java.util.ArrayList;
import java.util.List;
// ArrayList remove() method example
public class RemoveElementDemo
{
   public static void main(String[] args)
   {
      ArrayList fruits = new ArrayList<>(Arrays.asList("Apple", "Banana", "Cherry", "Dates"));
      System.out.println(fruits);
      fruits.remove(2);     // removes 'Cherry'
      System.out.println(fruits); 
      fruits.remove(10);    // index out of range exception
   }
}

Output:

[Apple, Banana, Cherry, Dates]
[Apple, Banana, Dates]
Exception in thread “main” java.lang.IndexOutOfBoundsException: Index: 10, Size: 3
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.remove(Unknown Source)
at RemoveElementDemo.main(RemoveElementDemo.java:12)

Also read – delete specified integer from array in java

Now we will see how to remove object from ArrayList using remove(Object o) method. This method removes only first occurrence.

import java.util.ArrayList;
import java.util.Arrays;
// ArrayList remove() method example
public class RemoveElementExample
{
   public static void main(String[] args) throws CloneNotSupportedException
   {
      ArrayList fruits = new ArrayList<>(Arrays.asList("Apple", "Banana", "Cherry", "Dates"));
      System.out.println(fruits); 
      fruits.remove("Cherry");  // element is present
      System.out.println(fruits); 
      fruits.remove("Pineapple");   // element is not present
      System.out.println(fruits);
   }
}

Output:

[Apple, Banana, Cherry, Dates]
[Apple, Banana, Dates]
[Apple, Banana, Dates]

NOTE:

ArrayList.remove() is not recommended to use while iterating over elements. Because it may lead to ConcurrentModificationException. Instead use Iterator.remove() method

Remove an element from collection using java Iterator

Here we will learn how to remove an element from collection using Iterator object,

import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
public class IteratorRemoveDemo
{
   public static void main(String[] args)
   {
      List<Integer> li = new ArrayList<>();
      li.add(10);
      li.add(20);
      li.add(30);
      li.add(07);
      li.add(06);
      Iterator ite = li.iterator();
      while(ite.hasNext())
      {
         int a = (Integer)ite.next();
         if(a < 10)
         {
            ite.remove();
         }
      }
      System.out.println("After removing elements from ArrayList : " + li);
   }
}

Output:

After removing elements from ArrayList : [10, 20, 30]

Meanwhile we can remove all occurrences of element. For that we have to use removeAll() method. Here’s the java program,

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
public class RemoveAllExample
{
   public static void main(String[] args) 
   {
      ArrayList fruits = new ArrayList<>(Arrays.asList("Apple", "Banana", "Apple", "Dates", "Apple"));
      System.out.println(fruits);
      fruits.removeAll(Collections.singleton("Apple"));
      System.out.println(fruits);
   }
}

Output:

[Apple, Banana, Apple, Dates, Apple]
[Banana, Dates]