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

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.

Also read – strings in java

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.Arrays;

public class ArrayListRemoveIndex 
{
   public static void main(String[] args) 
   {
      ArrayList<String> players = new ArrayList<String>(Arrays.asList("virat", "dhoni", "rohit"));
      System.out.println("Before using ArrayList.remove(int index) method: ");
      System.out.println(players);
      // remove element at 1 index
      players.remove(1);
      System.out.println("After using ArrayList.remove(int index) method: ");
      System.out.println(players);
   }
}


Output:

Before using ArrayList.remove(int index) method: [virat, dhoni, rohit]

After using ArrayList.remove(int index) method: [virat, rohit]


java arraylist remove element by value

Now let’s see example on remove element by value from arraylist

import java.util.ArrayList;
import java.util.Arrays;

public class ArraylistRemoveElementByValue 
{
   public static void main(String[] args) 
   {
      ArrayList<String> fruits = new ArrayList<String>(Arrays.asList("mango", "apple", "banana", "orange"));
      System.out.println("Before - arraylist remove element by value: ");
      System.out.println(fruits);
      // remove element by value
      fruits.remove("apple");
      System.out.println("After - arraylist remove element by value: ");
      System.out.println(fruits);
   }
}


Output:

Before – arraylist remove element by value: [mango, apple, banana, orange]

After – arraylist remove element by value: [mango, banana, orange]


arraylist remove object

Now we will see how to remove object 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.

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

import java.util.ArrayList;

public class ArrayListRemoveObjectMethod 
{
   public static void main(String[] args) 
   {
      ArrayList<String> animals = new ArrayList<String>(5);
      // using add() method to add values
      animals.add("Giraffe");
      animals.add("Elephant");
      animals.add("Fox");
      animals.add("Lion");
      animals.add("Elephant");
      System.out.println("Before using ArrayList.remove(Object) method size of ArrayList: " + animals.size());
      for(String str : animals) 
      {
         System.out.println(str);
      }
      animals.remove("Elephant");
      System.out.println("After using ArrayList.remove(Object) method size of ArrayList: " + animals.size());
      for(String str : animals) 
      {
         System.out.println(str);
      }
   }
}


Output:

Before using ArrayList.remove(Object) method size of ArrayList: 5
Giraffe
Elephant
Fox
Lion
Elephant
After using ArrayList.remove(Object) method size of ArrayList: 4
Giraffe
Fox
Lion
Elephant

NOTE:

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

Remove using java Iterator

How to remove element from arraylist in java while iterating

Here we will learn how to remove an element from collection 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<String> animals = new ArrayList<>();
      animals.add("Giraffe");
      animals.add("Elephant");
      animals.add("Cheetah");
      animals.add("Tiger");
      animals.add("Lion");
      animals.add("Jaguar");
      // print ArrayList before removing a element
      System.out.println("Before removing an element using Iterator's remove() method: ");
      System.out.println(animals);
      // remove element using ArrayList's remove method
      Iterator<String> iterate = animals.iterator();
      while(iterate.hasNext()) 
      {
         String str = iterate.next();
         if(str.equals("Giraffe")) 
         {
            iterate.remove();
         }
      }
      // print ArrayList after removing an element
      System.out.println("After removing an element using Iterator's remove() method: ");
      System.out.println(animals);
   }
}


Output:

Before removing an element using Iterator’s remove() method:
[Giraffe, Elephant, Cheetah, Tiger, Lion, Jaguar]
After removing an element using Iterator’s remove() method:
[Elephant, Cheetah, Tiger, Lion, Jaguar]


Reference – docs oracle


In java 8

In java 8 we can remove from arraylist using removeIf() method. removeIf() method removes all of the elements of this collection that satisfy the given predicate.

import java.util.ArrayList;
import java.util.Arrays;

public class Java8ArrayListRemove 
{
   public static void main(String[] args) 
   {
      ArrayList<String> fruits = new ArrayList<String>(Arrays.asList("mango", "apple", "banana", "orange"));
      System.out.println(fruits);              
      fruits.removeIf(str -> str.equals("apple"));                 
      System.out.println(fruits);
   }
}


Output:

[mango, apple, banana, orange]

[mango, banana, orange]


How to remove multiple elements from arraylist in java

To remove multiple objects from arraylist in java removeAll() method can be used.

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

In the below example there are two lists. In the first list we have four elements and in the second list we have two elements.

Now in the next step removeAll() method is called of first arraylist and second arraylist is passed as an argument to removeAll() method.

Here’s the example,

import java.util.ArrayList;
import java.util.List;

public class RemoveMultipleElements 
{
   public static void main(String[] args) 
   {
      List<String> animals = new ArrayList<>(); 
      animals.add("lion");
      animals.add("tiger");
      animals.add("cheetah");
      animals.add("jaguar");
      System.out.println("Before - remove multiple elements from arraylist in java: ");
      animals.forEach(System.out::println);
      List<String> li = new ArrayList<>();
      li.add("lion");
      li.add("cheetah");
      animals.removeAll(li);
      System.out.println("After - remove multiple elements from arraylist in java: ");
      animals.forEach(System.out::println);
   }
}


Output:

Before – remove multiple elements from arraylist in java:
lion
tiger
cheetah
jaguar
After – remove multiple elements from arraylist in java:
tiger
jaguar


remove last element from arraylist java

To remove last element from arraylist in java remove(int index) of List class is used.

remove(int index) method returns the element previously at the specified position and throws,

  • UnsupportedOperationException – if the remove operation is not supported by this.
  • listIndexOutOfBoundsException – if the index is out of range(index < 0 || index >= size())

Here’s the example,

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ArrayListRemoveLastElement 
{
   public static void main(String[] args) 
   {
      List<String> al = new ArrayList<>();
      al.addAll(Arrays.asList("Lion", "Tiger", "Cheetah", "Jaguar", "Elephant"));
      System.out.println("Before: " + al);
      int lastElement = al.size() - 1;
      al.remove(lastElement);
      System.out.println("After: " + al);
   }
}


Output:

Before: [Lion, Tiger, Cheetah, Jaguar, Elephant]
After: [Lion, Tiger, Cheetah, Jaguar]