Use the removeAll() method of the ArrayList class to remove specified elements from an array list in Java
In Java, ArrayList is a very commonly used data structure used to store and operate a set of objects. . Sometimes we need to remove specific elements from ArrayList. The ArrayList class provides the removeAll() method to help us achieve this requirement.
The removeAll() method is to remove all elements in another collection contained in ArrayList. Its declaration is as follows:
public boolean removeAll(Collection<?> c)
This method will remove elements from the ArrayList that are the same as the elements in the specified collection c. The "same" here refers to judging based on the equals() method of the element.
The following is a sample code that uses the removeAll() method to remove specified elements from an ArrayList:
import java.util.ArrayList; public class RemoveExample { public static void main(String[] args) { // 创建一个ArrayList对象 ArrayList<Integer> numbers = new ArrayList<>(); // 向numbers中添加一些元素 numbers.add(1); numbers.add(2); numbers.add(3); numbers.add(4); numbers.add(5); // 创建一个要移除的集合 ArrayList<Integer> toBeRemoved = new ArrayList<>(); toBeRemoved.add(2); toBeRemoved.add(4); // 使用removeAll()方法移除指定元素 numbers.removeAll(toBeRemoved); // 输出移除后的ArrayList for (Integer num : numbers) { System.out.println(num); } } }
Run the above code, you will get the following output:
1 3 5
Above In the example, we create an ArrayList object containing integers numbers
, and then create another ArrayList object toBeRemoved
that contains the values to be moved from numbers
elements to be removed. Then, we call the removeAll()
method of the numbers
object and pass in toBeRemoved
as a parameter. This method will remove all elements from numbers
that are identical to elements in toBeRemoved
. Finally, we use an enhanced for loop to traverse and output the elements of numbers
to confirm that the specified element has been successfully removed.
It should be noted that the removeAll() method will only remove elements that are the same as the elements in the specified collection. Even if there are other elements in the ArrayList that are the same as the specified element, they will not be removed. In addition, if the incoming collection is an empty collection, or there are no elements in the ArrayList that are the same as the elements in the collection, the removeAll() method will not modify the ArrayList.
In summary, the removeAll() method of the ArrayList class provides a convenient way to remove specified elements from an array list in Java. We simply create a collection containing the elements to be removed and pass it as a parameter to the removeAll() method. By using this method rationally, we can operate ArrayList more flexibly to achieve our business needs.
The above is the detailed content of Remove specified elements from an array list in Java using the removeAll() method of the ArrayList class. For more information, please follow other related articles on the PHP Chinese website!