当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java ArrayList retainAll()用法及代码示例


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

public boolean retainAll(Collection C)

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

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

下面的程序用于说明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<String> bags = new ArrayList<String>(); 
  
        // Add values in the bags list. 
        bags.add("pen"); 
        bags.add("pencil"); 
        bags.add("paper"); 
  
        // Creating another array list 
        ArrayList<String> boxes = new ArrayList<String>(); 
  
        // 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<String> bags = new HashSet<String>(); 
  
        // Add values in the bags Set. 
        bags.add("pen"); 
        bags.add("ink"); 
        bags.add("paper"); 
  
        // Creating another empty array list 
        ArrayList<String> boxes = new ArrayList<String>(); 
  
        // 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<Integer> list1 = null; 
  
        /// Creating another empty array list 
        ArrayList<String> list2 = new ArrayList<String>(); 
  
        // 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://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#retainAll(java.util.Collection)



相关用法


注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 ArrayList retainAll() method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。