Java List removeAll() method removes all of its elements that are also present in the given list. The method throws UnsupportedOperationException if the operation is not supported by the list.
Java List removeAll()方法将删除其所有在给定列表中也存在的元素。 如果列表不支持该操作,则该方法将引发UnsupportedOperationException。
If the given collection is null, NullPointerException is thrown.
如果给定的集合为null,则抛出NullPointerException 。
This method returns true
if the list is changed, otherwise false
.
如果更改列表,则此方法返回true
,否则返回false
。
Java列表removeAll()示例 (Java List removeAll() Examples)
Let’s look at some examples of removeAll() method with different types of list implementations.
让我们看一下带有不同类型的列表实现的removeAll()方法的一些示例。
1. ArrayList removeAll()示例 (1. ArrayList removeAll() Example)
List<String> list = new ArrayList<>();
list.add("A");
list.add("B");
list.add("C");
list.add("C");
list.add("B");
list.add("A");
System.out.println(list);
List<String> removeList = List.of("A", "B");
boolean isRemoved = list.removeAll(removeList);
System.out.println(list)