📌  相关文章
📜  Java中的 ArrayList retainAll() 方法

📅  最后修改于: 2022-05-13 01:55:25.152000             🧑  作者: Mango

Java中的 ArrayList retainAll() 方法

ArrayListretainAll()方法用于移除所有未包含在指定集合中的数组列表元素,或保留当前ArrayList 实例中与作为参数传递给该方法的集合列表中的所有元素匹配的所有匹配元素。
句法:

public boolean retainAll(Collection C)

参数: C是包含要保留在列表中的元素的集合。
返回值:如果由于调用 else false 导致列表完全更改,则该方法返回布尔值 true。
例外:

  1. ClassCastException:如果此 ArrayList 的某个元素的类与 Passed 集合不兼容。这是可选的。
  2. NullPointerException:如果列表包含空元素并且传递的集合不允许空元素,或者指定的集合为空。这也是可选的。

以下程序用于说明Java.util.ArrayList.retainAll() 方法:
程序 1:将 ArrayList 集合作为参数传递给方法。

// Java code to illustrate retainAll() method
import java.util.ArrayList;
public class GFG {
    public static void main(String[] args)
    {
        // Creating an empty array list
        ArrayList bags = new ArrayList();
  
        // Add values in the bags list.
        bags.add("pen");
        bags.add("pencil");
        bags.add("paper");
  
        // Creating another array list
        ArrayList boxes = new ArrayList();
  
        // Add values in the boxes list.
        boxes.add("pen");
        boxes.add("paper");
        boxes.add("books");
        boxes.add("rubber");
  
        // Before Applying method print both lists
        System.out.println("Bags Contains :" + bags);
        System.out.println("Boxes Contains :" + boxes);
  
        // Apply retainAll() method to boxes passing bags as parameter
        boxes.retainAll(bags);
  
        // Displaying both the lists after operation
        System.out.println("\nAfter Applying retainAll()"+
        " method to Boxes\n");
        System.out.println("Bags Contains :" + bags);
        System.out.println("Boxes Contains :" + boxes);
    }
}
输出:
Bags Contains :[pen, pencil, paper]
Boxes Contains :[pen, paper, books, rubber]

After Applying retainAll() method to Boxes

Bags Contains :[pen, pencil, paper]
Boxes Contains :[pen, paper]

程序 2:将不同于 ArrayList 的 Collection 作为参数传递给方法。

// Program Demonstrate retainAll() method With
// Collection different then ArrayList as a parameter of the method
import java.util.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Creating an empty array list
        HashSet bags = new HashSet();
  
        // Add values in the bags Set.
        bags.add("pen");
        bags.add("ink");
        bags.add("paper");
  
        // Creating another empty array list
        ArrayList boxes = new ArrayList();
  
        // Add values in the boxes list.
        boxes.add("pen");
        boxes.add("paper");
        boxes.add("books");
        boxes.add("rubber");
        boxes.add("ink");
  
        // Before Applying method print both list and set.
        System.out.println("Bags Contains :" + bags);
        System.out.println("Boxes Contains :" + boxes);
  
        // Apply retainAll() method to boxes passing bags as parameter
        boxes.retainAll(bags);
  
        // Displaying both the lists after operation
        System.out.println("\nAfter Applying retainAll()" + 
        " method to Boxes\n");
        System.out.println("Bags Contains :" + bags);
        System.out.println("Boxes Contains :" + boxes);
    }
}
输出:
Bags Contains :[paper, ink, pen]
Boxes Contains :[pen, paper, books, rubber, ink]

After Applying retainAll() method to Boxes

Bags Contains :[paper, ink, pen]
Boxes Contains :[pen, paper, ink]

程序 3:说明 retainAll() 方法抛出的错误

// Program to illustrate error thrown by retainAll() method
import java.util.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Creating an empty array list
        ArrayList list1 = null;
  
        /// Creating another empty array list
        ArrayList list2 = new ArrayList();
  
        // Add values in the list2 list.
        list2.add("pen");
        list2.add("paper");
        list2.add("books");
        list2.add("rubber");
  
        // Before Applying method print both lists
        System.out.println("list1 Contains :" + list1);
        System.out.println("list2 Contains :" + list2);
  
        // Apply retainAll() method to list2 passing list1 as parameter
        list2.retainAll(list1);
  
        // Displaying both the lists after operation
        System.out.println("\nAfter Applying retainAll()"+ 
        " method to list2\n");
        System.out.println("list1 Contains :" + list1);
        System.out.println("list2 Contains :" + list2);
    }
}

输出:

list1 Contains :null
list2 Contains :[pen, paper, books, rubber]

运行时错误:

Exception in thread "main" java.lang.NullPointerException
    at java.util.Objects.requireNonNull(Objects.java:203)
    at java.util.ArrayList.retainAll(ArrayList.java:714)
    at GFG.main(GFG.java:26)

参考:https: Java Java.util.Collection)