Home > Java > javaTutorial > body text

Use java's ArrayList.removeAll() function to delete specified elements in ArrayList

PHPz
Release: 2023-07-24 12:22:47
Original
1279 people have browsed it

Use Java's ArrayList.removeAll() function to delete specified elements in ArrayList

ArrayList is a commonly used collection class in Java, which provides a wealth of methods to operate and manage elements. During the development process, sometimes we need to delete specific elements in the ArrayList. The ArrayList class in Java provides the removeAll() function, which can be used to implement this function.

The removeAll() function is to remove all elements in the ArrayList that appear in the specified collection. We can delete specified elements in the ArrayList by passing in another ArrayList as a parameter.

The following is a sample code that demonstrates how to use the removeAll() function of ArrayList to delete specified elements in the ArrayList:

import java.util.ArrayList;

public class RemoveElements {

    public static void main(String[] args) {
        // 创建一个ArrayList对象
        ArrayList<String> list = new ArrayList<>();

        // 添加元素到ArrayList中
        list.add("apple");
        list.add("banana");
        list.add("orange");
        list.add("grape");
        list.add("watermelon");

        // 打印初始的ArrayList
        System.out.println("初始的ArrayList:");
        for (String element : list) {
            System.out.println(element);
        }
        System.out.println();

        // 创建一个新的ArrayList,用于存储要删除的元素
        ArrayList<String> toBeRemoved = new ArrayList<>();
        toBeRemoved.add("apple");
        toBeRemoved.add("orange");

        // 使用removeAll()函数删除ArrayList中的指定元素
        list.removeAll(toBeRemoved);

        // 打印删除指定元素后的ArrayList
        System.out.println("删除指定元素后的ArrayList:");
        for (String element : list) {
            System.out.println(element);
        }
    }
}
Copy after login

In the above code, we first create an ArrayList object and add some elements. Then create another ArrayList object toBeRemoved to store the elements to be removed. Next, we use the removeAll() function of ArrayList to remove the elements in toBeRemoved from the list. Finally, we print the ArrayList after deleting the specified element to verify whether the deletion operation is successful.

Run the above code, the output result is as follows:

初始的ArrayList:
apple
banana
orange
grape
watermelon

删除指定元素后的ArrayList:
banana
grape
watermelon
Copy after login

You can see that after processing by the removeAll() function, the specified element in the ArrayList has been successfully deleted.

To summarize, using the removeAll() function of Java's ArrayList can easily delete specified elements in the ArrayList. By passing in an ArrayList containing the elements to be deleted as a parameter, we can delete all elements in the list that are the same as the parameter list. This function can provide efficient and convenient deletion operations when processing array lists with a large number of elements.

The above is the detailed content of Use java's ArrayList.removeAll() function to delete specified elements in ArrayList. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template