How to remove element from arraylist in java

Let’s learn arraylist remove and 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 given 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)
   {
      List<Integer> li = new ArrayList<>(); 
      li.add(90); 
      li.add(50); 
      li.add(60); 
      li.add(7); 
      li.add(8);
      // removes element 50
      li.remove(1);
      // element 60 is moved one back to its position 
      li.remove(1);
      System.out.println("New ArrayList : " + li);
   }
}


Output:

New ArrayList : [90, 7, 8]

Also read – delete specified integer from array in java

Now we will see how to remove object from ArrayList using remove(Object o) method.

// remove object from arraylist java
import java.util.ArrayList;
import java.util.Arrays;
// ArrayList remove() method example
public class RemoveElementExample
{
   public static void main(String[] args)
   {
      List li<Integer> = new ArrayList<>(); 
      li.add(90); 
      li.add(50); 
      li.add(60); 
      li.add(7); 
      li.add(8);
      // call to remove(Object) removes element 1 
      li.remove(new Integer(7));
      // call to remove(Object) removes element 2 
      li.remove(new Integer(8));
      System.out.println("New ArrayList : " + li);
   }
}


Output:

New ArrayList : [90, 50, 60]

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 li = new ArrayList<>(); 
      li.add(50); 
      li.add(60); 
      li.add(70); 
      li.add(8); 
      li.add(9);
      // Iterator.remove() 
      Iterator itr = li.iterator();
      while(itr.hasNext()) 
      { 
          int num = (Integer)itr.next(); 
          if(num < 10)
          {
              itr.remove();
          }
      }
      System.out.println("New ArrayList : " + li);
   }
}


Output:

New ArrayList : [50, 60, 70]