CopyOnWriteArrayList的addAllAbsent(E e)方法將指定集合中尚未包含在此列表中的所有元素追加到此列表的末尾,並按指定集合的迭代器返回的順序進行添加。
用法:
public int addAllAbsent(Collection<E> collection)
參數:該函數接受單個強製性參數集合,該集合指定了不存在的附加到列表的集合。
返回值:該函數返回一個整數值,該整數值是在列表中添加元素時的數字。
異常:如果指定的集合為null,則此方法將引發NullPointerException。
以下示例程序旨在說明addAllAbsent()函數:
示例1:
// Java Program to illustrate the CopyOnWriteArrayList
// addAllAbsent() method in Java
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// create object of CopyOnWriteArrayList
CopyOnWriteArrayList<Integer> ArrLis
= new CopyOnWriteArrayList<Integer>();
// Add elements
ArrLis.add(2);
ArrLis.add(3);
ArrLis.add(4);
ArrLis.add(7);
// print CopyOnWriteArrayList
System.out.println("CopyOnWriteArrayList: "
+ ArrLis);
// creating an Empty Integer ArrayList
ArrayList<Integer> arr = new ArrayList<Integer>(4);
// using add() to initialize values
// [1, 2, 3, 4]
arr.add(1);
arr.add(2);
arr.add(3);
arr.add(4);
// list initially
System.out.println("The collection to be appended: "
+ arr);
// Append the list arr in the list ArrLis
// using addAllAbsent() method
System.out.println("\nNumber of elements appended"
+ " using addAllAbsent() method: "
+ ArrLis.addAllAbsent(arr));
// print CopyOnWriteArrayList
System.out.println("Modified CopyOnWriteArrayList: "
+ ArrLis);
}
}
輸出:
CopyOnWriteArrayList: [2, 3, 4, 7] The collection to be appended: [1, 2, 3, 4] Number of elements appended using addAllAbsent() method: 1 Modified CopyOnWriteArrayList: [2, 3, 4, 7, 1]
示例2:演示NullPointerException
// Java Program to illustrate the CopyOnWriteArrayList
// addAllAbsent() method in Java
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// create object of CopyOnWriteArrayList
CopyOnWriteArrayList<Integer> ArrLis
= new CopyOnWriteArrayList<Integer>();
// Add elements
ArrLis.add(2);
ArrLis.add(3);
ArrLis.add(4);
ArrLis.add(7);
// print CopyOnWriteArrayList
System.out.println("CopyOnWriteArrayList: "
+ ArrLis);
// creating a null Integer ArrayList
ArrayList<Integer> arr = null;
// list initially
System.out.println("The collection to be appended: "
+ arr);
try {
// Append the list arr in the list ArrLis
// using addAllAbsent() method
System.out.println("\nTrying to append null collection\n");
ArrLis.addAllAbsent(arr);
}
catch (Exception e) {
System.out.println(e);
}
}
}
輸出:
CopyOnWriteArrayList: [2, 3, 4, 7] The collection to be appended: null Trying to append null collection java.lang.NullPointerException
相關用法
- Java CopyOnWriteArrayList set()用法及代碼示例
- Java CopyOnWriteArrayList remove()用法及代碼示例
- Java CopyOnWriteArrayList addAll()用法及代碼示例
- Java CopyOnWriteArrayList retainAll()用法及代碼示例
- Java CopyOnWriteArrayList equals()用法及代碼示例
- Java CopyOnWriteArrayList forEach()用法及代碼示例
- Java CopyOnWriteArrayList removeIf()用法及代碼示例
- Java CopyOnWriteArrayList subList()用法及代碼示例
- Java CopyOnWriteArrayList removeAll()用法及代碼示例
- Java CopyOnWriteArrayList get()用法及代碼示例
- Java CopyOnWriteArrayList add()用法及代碼示例
- Java CopyOnWriteArrayList contains()用法及代碼示例
- Java CopyOnWriteArrayList lastIndexOf()用法及代碼示例
- Java CopyOnWriteArrayList listIterator()用法及代碼示例
- Java CopyOnWriteArrayList size()用法及代碼示例
注:本文由純淨天空篩選整理自SandySharma大神的英文原創作品 CopyOnWriteArrayList addAllAbsent() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。