How to remove element from ArrayList in Java?


The List interface is a member of Java Collections Framework. It extends Collection and stores a sequence of elements. ArrayList and LinkedList are the most commonly used implementations of List interface. List provides user a precise control over where an element to be inserted in the List. These elements can be accessed by their index and are searchable.

The List provides two remove() methods to remove an element from list by providing the index of element or element itself.

Remove using Index.

E remove(int index)

Notes

  • 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.

Parameters

  • index - The index of the element to be removed.

Returns

The element previously at the specified position

Throws

  • UnsupportedOperationException - If the remove operation is not supported by this list.

  • IndexOutOfBoundsException - If the index is out of range (index < 0 || index >= size()).

Remove using Object.

boolean remove(Object o)

Notes

  • Removes the first occurrence of the specified element from this list, if it is present.

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

  • Removes the element with the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))) (if such an element exists).

  • Returns true if this list contained the specified element (or equivalently, if this list changed as a result of the call).

Parameters

  • o - Element to be removed from this list, if present.

Returns

True if this list contained the specified element.

Throws

  • ClassCastException - If the type of the specified element is incompatible with this list (optional).

  • NullPointerException - If the specified element is null and this list does not permit null elements (optional).

  • UnsupportedOperationException - if the remove operation is not supported by this list.

Example 1

Following is the example showing the usage of remove() method to remove an element by index −

package com.tutorialspoint;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class CollectionsDemo {
   public static void main(String[] args) {
      List<Integer> list = new ArrayList<>(Arrays.asList(1,2,3,4,5,6,7,8,9,0));
      System.out.println("List: " + list);

      // remove item at index 5,
      list.remove(5);
     
      // updated list is not having 6
      System.out.println("Updated List: " + list);
   }
}

Output

This will produce the following result −

List: [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
Updated List: [1, 2, 3, 4, 5, 7, 8, 9, 0]

Example 2

Following is the example showing the usage of remove() method to remove an element by passing the element −

package com.tutorialspoint;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class CollectionsDemo {
   public static void main(String[] args) {
      List<Integer> list = new ArrayList<>(Arrays.asList(1,2,3,4,5,6,7,8,9,0));
      System.out.println("List: " + list);

      // remove the item 7,
      list.remove(Integer.valueOf(7));

      // updated list is not having 7
      System.out.println("Updated List: " + list);
   }
}

Output

This will produce the following result −

List: [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
Updated List: [1, 2, 3, 4, 5, 6, 8, 9, 0]

Updated on: 26-May-2022

305 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements