How to remove an element from ArrayList in Java?

There are multiple way to remove an element from ArrayList in Java.

From CopyOnWriteArrayList :-

  • First we create CopyOnWriteArrayList object with String type data.
  • Now we traverse each list elements one by one and storing in string.
  • Inside if condition, contains() method checks whether a string contains a sequence of characters.
  • If specific String found then call remove method to removing element from list.
  • Now print the list of elements.

Syntax :-  public boolean contains(CharSequence chars)

From removeIf() and contains() method:-

  • First we create ArrayList object with String type data.
  • Now we call removeIf method to remove all elements.
  • Inside removeIf we call contains() method checks whether a string contains a sequence of given characters.
  • If specific String found then removeIf() method to removing element from list.
  • Now print the list of elements.

Syntax :-  public boolean removeIf(Predicate filter)

From stream and filter method :-

  • First convert Arrays to list using Arrays.asList method.
  • Now get the Stream data from List using ArrayList.stream() method.
  • Inside filter method, contains() method checks whether a string contains a sequence of given characters and through not condition they will return false at specific condition.
  • Now collect all list of data from toList() method.
  • Print the list of elements.

From Method reference and removeIf() method :-

  • First we create ArrayList object with String type data and all elements.
  • Now we call removeIf method to remove all elements.
  • Inside removeIf() method we call internally equals method from method reference type to compare the given elements from list elements.
  • If it is equals,then they remove elements.
  • Now print the list of elements.
				
					import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Collectors;

public class RemoveElementFromList {

	public static void main(String[] args) {

		// from CopyOnWriteArrayList
		CopyOnWriteArrayList<String> listOne = new CopyOnWriteArrayList<String>();
		listOne.add("spring");
		listOne.add("servlet");
		listOne.add("javax");
		for (String k : listOne) {
			if (k.contains("x")) {
				listOne.remove(k);
			}
		}
		System.out.println("remove element from CopyOnWriteArrayList :-" + listOne);

		// from List and removeIf method
		List<String> listTwo = new ArrayList<String>();
		listTwo.add("exception");
		listTwo.add("hibernate");
		listTwo.add("servlet");
		listTwo.removeIf(e -> e.contains("v"));
		System.out.println("remove element from removeIf List method :-" + listTwo);

		// from stream and filter method
		List<String> listThree = Arrays.asList("collection", "javax", "thread");
		List<String> data = listThree.stream().filter(r -> !r.contains("x")).collect(Collectors.toList());
		System.out.println("remove element from stream java 8 :- " + data);

		// from stream and filter method
		List<String> list = new ArrayList<>();
		list.add("M");
		list.add("N");
		list.add("P");
		list.removeIf("N"::equals);
		System.out.println("from java 8 removeIf Method :- " + list);

	}

}
				
			

Output :-
remove element from CopyOnWriteArrayList :-[spring, servlet]
remove element from removeIf List method :-[exception, hibernate]
remove element from stream java 8 :- [collection, thread]
from java 8 removeIf Method :- [M, P]