Java CopyOnWriteArrayList addAllAbsent()方法及示例

Java CopyOnWriteArrayList addAllAbsent()方法及示例

CopyOnWriteArrayListaddAllAbsent(E e) 方法将指定集合中尚未包含在此列表中的所有元素,按照指定集合的迭代器所返回的顺序,追加到此列表的末尾。

语法

public int addAllAbsent(Collection<E> collection)

参数: 该函数接受一个强制性参数 collection ,该参数指定了要追加到列表中的集合,如果它不存在的话。

返回值: 该函数返回一个整数值,即在列表中添加的元素数量。

异常情况。如果指定的集合为空,该方法会抛出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

参考资料: https://docs.oracle.com/javase/9/docs/api/java/util/concurrent/CopyOnWriteArrayList.html#addAllAbsent-java.util.Collection-

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程