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


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


Java中的Java.util.concurrent.CopyOnArrayList.retainAll()方法用于仅保留列表中包含在特定集合中的元素。

用法:

 public boolean retainAll(Collection col) 

参数:此方法接受具有收集类型的强制性参数col。这是要保留在此向量中的元素的集合。


返回类型:如果由于调用else而导致列表完全更改,则此方法返回布尔值true。

异常:如果指定的集合为NULL,则此方法引发NullPointerException。

下面的程序演示了Java中的CopyOnArrayList的retainAll()方法:

示例1:该程序涉及Integer类型的CopyOnArraylist retainAll()方法:

// Java Program to illustrate CopyOnArrayList 
// retainAll() method 
import java.util.concurrent.CopyOnWriteArrayList; 
import java.util.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
        // create object of CopyOnWriteArrayList 
        CopyOnWriteArrayList<Integer> ArrLis1 
            = new CopyOnWriteArrayList<Integer>(); 
  
        // Add elements 
        ArrLis1.add(34); 
        ArrLis1.add(66); 
        ArrLis1.add(72); 
        ArrLis1.add(10); 
  
        // print CopyOnWriteArrayList 
        System.out.println("CopyOnWriteArrayList1: "
                           + ArrLis1); 
  
        // create object of CopyOnWriteArrayList 
        CopyOnWriteArrayList<Integer> ArrLis2 
            = new CopyOnWriteArrayList<Integer>(); 
  
        // Add elements 
        ArrLis2.add(10); 
        ArrLis2.add(66); 
        ArrLis2.add(97); 
  
        // print CopyOnWriteArrayList 
        System.out.println("CopyOnWriteArrayList2: "
                           + ArrLis2); 
  
        // Applying the function 
        ArrLis2.retainAll(ArrLis1); 
  
        // print CopyOnWriteArrayLists 
  
        System.out.println("After applying retainAll()"
                           + " method on ArrLis2"); 
        System.out.println("CopyOnWriteArrayList1: "
                           + ArrLis1); 
        System.out.println("CopyOnWriteArrayList2: "
                           + ArrLis2); 
    } 
}
输出:
CopyOnWriteArrayList1: [34, 66, 72, 10]
CopyOnWriteArrayList2: [10, 66, 97]
After applying retainAll() method on ArrLis2
CopyOnWriteArrayList1: [34, 66, 72, 10]
CopyOnWriteArrayList2: [10, 66]

示例2:该程序涉及String类型的CopyOnArraylist retainAll()方法:

// Java Program to illustrate CopyOnArrayList 
// retainAll() method 
  
import java.util.concurrent.CopyOnWriteArrayList; 
import java.util.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
        CopyOnWriteArrayList<String> ArrLis1 
            = new CopyOnWriteArrayList<String>(); 
  
        // Add elements 
        ArrLis1.add("geeks"); 
        ArrLis1.add("shaan"); 
        ArrLis1.add("gfg"); 
        ArrLis1.add("programming"); 
  
        // print CopyOnWriteArrayList 
        System.out.println("CopyOnWriteArrayList1: "
                           + ArrLis1); 
  
        // create object of CopyOnWriteArrayList 
        CopyOnWriteArrayList<String> ArrLis2 
            = new CopyOnWriteArrayList<String>(); 
  
        // Add elements 
        ArrLis2.add("geeks"); 
        ArrLis2.add("gfg"); 
        ArrLis2.add("qwe"); 
  
        // print CopyOnWriteArrayList 
        System.out.println("CopyOnWriteArrayList2: "
                           + ArrLis2); 
  
        // Applying the function 
        ArrLis2.retainAll(ArrLis1); 
  
        // print CopyOnWriteArrayLists 
  
        System.out.println("After applying retainAll()"
                           + " method on ArrLis2"); 
        System.out.println("CopyOnWriteArrayList1: "
                           + ArrLis1); 
        System.out.println("CopyOnWriteArrayList2: "
                           + ArrLis2); 
    } 
}
输出:
CopyOnWriteArrayList1: [geeks, shaan, gfg, programming]
CopyOnWriteArrayList2: [geeks, gfg, qwe]
After applying retainAll() method on ArrLis2
CopyOnWriteArrayList1: [geeks, shaan, gfg, programming]
CopyOnWriteArrayList2: [geeks, gfg]


相关用法


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