List is an interface class in Java and there are several implementations of this, for example ArrayList, LinkedList and many more. In below examples we will reference one of the implementation, but the interface methods are mostly supported in all implementations unless the implementation does not support these methods like if it's an un modifiable list etc.

To remove an element from List using it's index, we can use the List.remove(int i) method where you can pass the index of an element.

public void deleteUsingIndexRemoveMethod() { List<Integer> arrayList = new ArrayList<>(List.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); System.out.println("BEFORE - arrayList size : "+ arrayList.size() + " , elements : "+arrayList); arrayList.remove(0); System.out.println("AFTER - arrayList size : "+ arrayList.size() + " , elements : "+arrayList); }

In the above example, we are removing element at index 0 from the list, that is the first element from the list. When you run the program, you will see below output.

BEFORE - arrayList size : 10 , elements : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
AFTER - arrayList size : 9 , elements : [2, 3, 4, 5, 6, 7, 8, 9, 10]
Note that this method throws IndexOutOfBoundsException if the index you are trying to remove is out of range (index < 0 || index >= size())

To delete an element from List using it's value, you can use List.remove(Object element) method where you pass element that you want to remove as parameter.

public void deleteUsingRemoveIfMethod() { List<String> arrayList = new ArrayList<>(List.of("a", "b", "c", "a")); System.out.println("BEFORE - arrayList size : "+ arrayList.size() + " , elements : "+arrayList); arrayList.removeIf(s -> "a".equals(s)); System.out.println("AFTER - arrayList size : "+ arrayList.size() + " , elements : "+arrayList); }

In the above example, we are removing element with value as "a" from the list. Note that this method only removes the first occurrence of the element. When you run the program, you will see below output.

BEFORE - arrayList size : 4 , elements : [a, b, c, a]
AFTER - arrayList size : 3 , elements : [b, c, a]

To find the element to remove from list, it checks for element from list that is equal to the element that you are passing as parameter, so it's important that Object you are using have equals(Object other) and hashCode() methods of super class Object are overridden, otherwise you it checks if the object in the list is the exact reference of object in memory that you are passing.
See this article for implementing equals and hashCode Object's equal and hashCode

List.remove(Object element) method checks all elements and and removes the first element that is equal to the element you are passing as parameter.

The removeIf method takes a predicate as an argument where you can pass a lambda function, so you can pass the condition to remove an element from the list.

One major difference between previous remove(Object element) and this method is that condition for deletion of an element can be customized, and it deletes all occurrences of elements that satisfies the predicate condition.

public void deleteUsingRemoveIfMethod() { List<String> arrayList = new ArrayList<>(List.of("a", "b", "c", "a")); System.out.println("BEFORE - arrayList size : "+ arrayList.size() + " , elements : "+arrayList); arrayList.removeIf(s -> "a".equals(s)); System.out.println("AFTER - arrayList size : "+ arrayList.size() + " , elements : "+arrayList); }

When you run the above program, it will produce the below output, as you notice it removed all occurrences of element "a".

BEFORE - arrayList size : 4 , elements : [a, b, c, a]
AFTER - arrayList size : 2 , elements : [b, c]

You can obtain Iterator using iterator() method or listIterator() from List, then call remove() on Iterator.

public void deleteUsingIteratorMethod() { List<String> arrayList = new ArrayList<>(List.of("a", "b", "c", "a")); System.out.println("BEFORE - arrayList size : "+ arrayList.size() + " , elements : "+arrayList); Iterator<String> ite = arrayList.iterator(); while(ite.hasNext()){ String element = ite.next(); if("a".equals(element)) { ite.remove(); } } System.out.println("AFTER - arrayList size : "+ arrayList.size() + " , elements : "+arrayList); }

When you run above program, it will produce following output.

BEFORE - arrayList size : 4 , elements : [a, b, c, a]
AFTER - arrayList size : 2 , elements : [b, c]

Similar Articles

  1. How to loop through a List elements in different ways
  2. How to remove elements from a List while iterating