How to remove element from arraylist in java

Let’s learn 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.

remove() method removes the element at the specified position in the given list.

how to remove element from arraylist in java

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.

Also read – arraylist in java

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

import java.util.ArrayList;
import java.util.List;
public class ArrayListRemoveIndex
{
   public static void main(String[] args)
   {
      List<Integer> al = new ArrayList<>();
      al.add(56);
      al.add(28);
      al.add(39);
      al.add(59);
      al.add(82);
      System.out.println("Before using ArrayList.remove(int index) method: " + al);
      // removes element 28
      al.remove(1);
      // element 39 is now moved one position back
      // So element 39 is removed
      al.remove(1);
      System.out.println("After using ArrayList.remove(int index) method: " + al);
   }
}


Output:

Before using ArrayList.remove(int index) method: [56, 28, 39, 59, 82]
After using ArrayList.remove(int index) method: [56, 59, 82]

As you can see the output, the passed parameter is considered as index.

Remove (int index) method accepts index to be removed as parameter.


arraylist remove object

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

This method removes the first occurrence of the specified element from this list, if it is present.

If the list does not contain the element, it is unchanged.

Also read – hashset in java

Java.util.ArrayList.remove(Object) method example.

import java.util.ArrayList;
import java.util.List;
public class ArrayListRemoveObjectMethod
{
   public static void main(String[] args)
   {
      List<Integer> al = new ArrayList<>();
      al.add(56);
      al.add(28);
      al.add(39);
      al.add(59);
      al.add(82);
      System.out.println("Before using ArrayList.remove(Object) method size of ArrayList: " + al);
      // removes element 56
      al.remove(new Integer(56));
      // removes element 28
      al.remove(new Integer(28));
      System.out.println("After using ArrayList.remove(Object) method size of ArrayList: " + al);
   }
}


Output:

Before using ArrayList.remove(Object) method size of ArrayList: [56, 28, 39, 59, 82]
After using ArrayList.remove(Object) method size of ArrayList: [39, 59, 82]

NOTE:

ArrayList.remove() method is not recommended to use while iterating over elements. Because it will throw ConcurrentModificationException. Instead use Iterator’s remove() method.


Iterator.remove() method

Here we will learn how to remove an element using Iterator’s remove() method.

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class IteratorRemoveDemo
{
   public static void main(String[] args)
   {
      List<Integer> al = new ArrayList<>();
      al.add(56);
      al.add(28);
      al.add(39);
      al.add(59);
      al.add(82);
      System.out.println("Before removing element using Iterator remove() method: " + al);
      Iterator<Integer> iterate = al.iterator();
      while(iterate.hasNext())
      {
         int r = (Integer)iterate.next();
         if(r > 50)
         {
            iterate.remove();
         }
      }
      System.out.println("After removing element using Iterator remove() method: " + al);
   }
}


Output:

Before removing element using Iterator remove() method: [56, 28, 39, 59, 82]
After removing element using Iterator remove() method: [28, 39]


how to remove last element from arraylist in java

To remove last element from arraylist in java use two overloaded remove() method of ArrayList. They are,

  • remove(int index): accepts the index of the element to be removed. Pass index of last element to delete last element.
  • remove(Object o): takes input as object to be removed. If there is no duplicates in ArrayList then pass last element value to remove method.

Here’s the example on remove(int index) to last element from arraylist.

import java.util.ArrayList;
import java.util.List;
public class ArrayListRemoveLastElement
{
   public static void main(String[] args)
   {
      List<Integer> al = new ArrayList<>();
      al.add(56);
      al.add(28);
      al.add(39);
      al.add(59);
      al.add(82);
      // using size() method to find index of last element
      int index = al.size() - 1;
      // deleting last element by passing index
      al.remove(index);
      System.out.println("After remove last element from arraylist java: " + al);
   }
}


Output:

After remove last element from arraylist java: [56, 28, 39, 59]


Here’s the example on remove(Object o) to last element from arraylist.

import java.util.ArrayList;
import java.util.List;
public class ArrayListRemoveLastElement
{
   public static void main(String[] args)
   {
      List<Integer> al = new ArrayList<>();
      al.add(56);
      al.add(28);
      al.add(39);
      al.add(59);
      al.add(82);
      al.remove(new Integer(82));
      System.out.println("After remove last element from arraylist java: " + al);
   }
}


Output:

After remove last element from arraylist java: [56, 28, 39, 59]


how to remove all elements from arraylist in java

We can remove all elements from arraylist in java in two ways. One using clear() method and other using removeAll() method.

Both clear() and removeAll() method are defined in java.util.List and java.util.Collection interface.

Also read – remove element from array java

clear() method removes all of the elements from this list. The list willbe empty after this call returns.

removeAll() method removes from this list all of its elements that are contained in thespecified collection.

The time taken to remove all elements from ArrayList using clear() method is O(n).

Also read – continue statement in java

The time taken to remove all elements from ArrayList using remveAll() method is O(n ^ 2).

Let’s see how to remove all elements from arraylist in java.

import java.util.ArrayList;
public class RemoveAllElements
{
   public static void main(String[] args)
   {
      ArrayList<String> fruits = new ArrayList<>();
      fruits.add("pineapple");
      fruits.add("orange");
      fruits.add("apple");
      fruits.add("banana");
      fruits.add("watermelon");
      System.out.println("ArrayList before using clear() method: ");
      System.out.println(fruits);
      // empty an ArrayList using clear() method
      fruits.clear();
      System.out.println("ArrayList after using clear() method: ");
      System.out.println(fruits);
      ArrayList<String> colors = new ArrayList<>();
      colors.add("red");
      colors.add("blue");
      colors.add("green");
      colors.add("white");
      System.out.println("ArrayList before using removeAll() method: ");
      System.out.println(colors);
      // empty an ArrayList using removeAll() method
      colors.removeAll(colors);
      System.out.println("ArrayList after using removeAll() method: ");
      System.out.println(colors);
   }
}


Output:

ArrayList before using clear() method: [pineapple, orange, apple, banana, watermelon]
ArrayList after using clear() method: []
ArrayList before using removeAll() method: [red, blue, green, white]
ArrayList after using removeAll() method: []


Reference – docs oracle


how to remove element from arraylist in java while iterating

Let’s learn how to remove element from arraylist while iterating using Iterator’s remove() method.

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class ArrayListWhileIterating
{
   public static void main(String[] args)
   {
      List<String> fruits = new ArrayList<>();
      fruits.add("grapes");
      fruits.add("pineapple");
      fruits.add("apple");
      fruits.add("mango");
      fruits.add("banana");
      fruits.add("orange");
      // before removing any element
      System.out.println("Before - remove element from arraylist while iterating: ");
      System.out.println(fruits);
      Iterator<String> itr = fruits.iterator();
      while(itr.hasNext())
      {
         String strFruit = itr.next();
         if(strFruit.equals("apple"))
         {
            itr.remove();
         }
      }
      // after removing an element
      System.out.println("After - remove element from arraylist while iterating: ");
      System.out.println(fruits);
   }
}


Output:

Before – remove element from arraylist while iterating:
[grapes, pineapple, apple, mango, banana, orange]
After – remove element from arraylist while iterating:
[grapes, pineapple, mango, banana, orange]