How to remove an element from a Java List?

JavaObject Oriented ProgrammingProgramming

The List interface is a Collection and it stores a sequence of elements. ArrayList is the most popular implementation of the List interface. A list provides user has quite precise control over where an element to be inserted in the List. These elements are accessible by their index and are searchable. ArrayList is the most common implementation of List interface.

Elements can be removed from a List using multiple ways.

Way #1

Remove an element using its index.

Syntax

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 was 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()).

Way #2

Remove an element using it.

Syntax

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

  • 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

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

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

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

Example 1

Following is the example showing usage of remove() method to remove 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(0,1,2,3,4,5,6,7,8,9));
      System.out.println("List: " + list);
     
      list.remove(1);
      System.out.println("After remove(1), List: " + list);
   }
}

Output

This will produce the following result −

List: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
After remove(1), List: [0, 2, 3, 4, 5, 6, 7, 8, 9]

Example 2

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

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(0,1,2,3,4,5,6,7,8,9));
      System.out.println("List: " + list);
     
      list.remove(Integer.valueOf(5));
      System.out.println("After remove(Integer.valueOf(5)), List: " + list);
   }
}

Output

This will produce the following result −

List: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
After remove(Integer.valueOf(5)), List: [0, 1, 2, 3, 4, 6, 7, 8, 9]
raja
Updated on 26-May-2022 13:25:06

Advertisements