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]
相關用法
- Java LinkedTransferQueue retainAll()用法及代碼示例
- Java LinkedBlockingDeque retainAll()用法及代碼示例
- Java SortedSet retainAll()用法及代碼示例
- Java List retainAll()用法及代碼示例
- Java Vector retainAll()用法及代碼示例
- Java AbstractCollection retainAll()用法及代碼示例
- Java CopyOnWriteArrayList set()用法及代碼示例
- Java CopyOnWriteArrayList addAll()用法及代碼示例
- Java CopyOnWriteArrayList forEach()用法及代碼示例
- Java CopyOnWriteArrayList remove()用法及代碼示例
- Java CopyOnWriteArrayList removeAll()用法及代碼示例
- Java CopyOnWriteArrayList removeIf()用法及代碼示例
- Java CopyOnWriteArrayList addAllAbsent()用法及代碼示例
- Java CopyOnWriteArrayList equals()用法及代碼示例
- Java CopyOnWriteArrayList subList()用法及代碼示例
注:本文由純淨天空篩選整理自kanakasrijaathukuri大神的英文原創作品 CopyOnWriteArrayList retainAll() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。