Home  >  Article  >  Java  >  Use the retainAll() method of the ArrayList class to obtain the intersection of two array lists

Use the retainAll() method of the ArrayList class to obtain the intersection of two array lists

WBOY
WBOYOriginal
2023-07-25 14:02:021866browse

Use the retainAll() method of the ArrayList class to obtain the intersection of two array lists

Array list is a very commonly used data structure in Java. Its flexibility and functionality make it an ideal choice for processing data. one. Java provides many built-in methods for operating and processing array lists. One of the retainAll() methods can be used to obtain the intersection between two array lists.

Before we begin, let us first understand the role of the retainAll() method. The retainAll() method is a member method of the ArrayList class, used to obtain the intersection between two array lists. It modifies the array list on which the method is called so that it retains only the same elements as in the argument array list, while removing other elements.

Next, let’s look at a simple sample code that demonstrates how to use the retainAll() method to get the intersection of two array lists:

import java.util.ArrayList;

public class IntersectionExample {
    public static void main(String[] args) {
        // 创建两个数组列表
        ArrayList<Integer> list1 = new ArrayList<>();
        ArrayList<Integer> list2 = new ArrayList<>();

        // 向数组列表中添加元素
        list1.add(1);
        list1.add(2);
        list1.add(3);
        list1.add(4);

        list2.add(3);
        list2.add(4);
        list2.add(5);
        list2.add(6);

        // 调用retainAll()方法获取交集
        list1.retainAll(list2);

        // 打印交集结果
        System.out.println("交集为:" + list1);
    }
}

In the above code, we first create Two ArrayList objects list1 and list2 and added some integer elements to them respectively. We then call the retainAll() method of list1 and pass list2 as a parameter to the method. After this method is executed, only the same elements as list2 are retained in list1, that is, the intersection. Finally, we print out the intersection result.

Run the above code and you will get the following output:

交集为:[3, 4]

As shown above, by using the retainAll() method of the ArrayList class, we can easily get the difference between the two array lists. intersection. This makes it easier for us to work with data.

It should be noted that the retainAll() method will modify the array list that calls this method instead of creating a new array list. If you don't want to modify the original array list, you can create a copy before calling the retainAll() method.

To summarize, the intersection between two array lists can be easily obtained using the retainAll() method of the ArrayList class. This is a powerful and practical way to process array lists in Java, which can greatly simplify our programming work.

I hope this article will be helpful for beginners to use the retainAll() method of the ArrayList class to obtain the intersection of two array lists.

The above is the detailed content of Use the retainAll() method of the ArrayList class to obtain the intersection of two array lists. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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