📜  Java中的 AbstractQueue addAll() 方法及示例

📅  最后修改于: 2022-05-13 01:54:46.989000             🧑  作者: Mango

Java中的 AbstractQueue addAll() 方法及示例

AbstractQueueaddAll(E e)方法将指定集合中的所有元素添加到此队列中。

句法:

public boolean addAll(Collection c)

参数:此方法接受一个强制参数集合,其中包含要添加到此队列的元素

返回:如果此队列因调用而更改,则该方法返回true

异常:此方法抛出以下异常:

  • IllegalStateException : 如果由于插入限制,此时不能添加所有元素
  • NullPointerException : 如果指定的集合包含空元素并且此队列不允许空元素,或者指定的集合为空
  • ClassCastException – 如果指定集合的元素的类阻止它被添加到此队列
  • IllegalArgumentException – 如果指定集合的元素的某些属性阻止它被添加到此队列,或者指定集合是此队列

下面的程序说明了 addAll() 方法:

方案一:

// Java program to illustrate the
// AbstractQueue addAll() method
import java.util.*;
import java.util.concurrent.LinkedBlockingQueue;
  
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
        // Creating object of AbstractQueue
        AbstractQueue
            AQ1 = new LinkedBlockingQueue();
  
        // Populating AQ1
        AQ1.add(10);
        AQ1.add(20);
        AQ1.add(30);
        AQ1.add(40);
        AQ1.add(50);
  
        // print AQ
        System.out.println("AbstractQueue1 contains : " + AQ1);
  
        AbstractQueue
            AQ2 = new LinkedBlockingQueue();
  
        // print AQ2 initially
        System.out.println("AbstractQueue2 initially contains : " + AQ2);
  
        // adds elements of AQ1 in AQ2
        AQ2.addAll(AQ1);
  
        System.out.println("AbstractQueue1 after addition contains : " + AQ2);
    }
}
输出:
AbstractQueue1 contains : [10, 20, 30, 40, 50]
AbstractQueue2 initially contains : []
AbstractQueue1 after addition contains : [10, 20, 30, 40, 50]

程序 2: IllegalStateException程序

// Java program to illustrate the
// AbstractQueue addAll() method
import java.util.*;
import java.util.concurrent.LinkedBlockingQueue;
  
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
        try {
            // Creating object of AbstractQueue
            AbstractQueue
                AQ1 = new LinkedBlockingQueue();
  
            // Populating AQ1
            AQ1.add(10);
            AQ1.add(20);
            AQ1.add(30);
            AQ1.add(40);
            AQ1.add(50);
  
            // print AQ
            System.out.println("AbstractQueue1 contains : " + AQ1);
  
            AbstractQueue
                AQ2 = new LinkedBlockingQueue(3);
  
            // print AQ2 initially
            System.out.println("AbstractQueue2 initially contains : " + AQ2);
  
            // adds elements of AQ1 in AQ2
            AQ2.addAll(AQ1);
  
            System.out.println("AbstractQueue1 after addition contains : " + AQ2);
        }
        catch (Exception e) {
            System.out.println("Exception: " + e);
        }
    }
}
输出:
AbstractQueue1 contains : [10, 20, 30, 40, 50]
AbstractQueue2 initially contains : []
Exception: java.lang.IllegalStateException: Queue full

程序 3: NullPointerException程序

// Java program to illustrate the
// AbstractQueue addAll() method
import java.util.*;
import java.util.concurrent.LinkedBlockingQueue;
  
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
        try {
            // Creating object of AbstractQueue
            AbstractQueue
                AQ1 = null;
  
            // print AQ
            System.out.println("AbstractQueue1 contains : " + AQ1);
  
            AbstractQueue
                AQ2 = new LinkedBlockingQueue(3);
  
            // print AQ2 initially
            System.out.println("AbstractQueue2 initially contains : " + AQ2);
  
            // adds elements of AQ1 in AQ2
            AQ2.addAll(AQ1);
  
            System.out.println("AbstractQueue1 after addition contains : " + AQ2);
        }
        catch (Exception e) {
            System.out.println("Exception: " + e);
        }
    }
}
输出:
AbstractQueue1 contains : null
AbstractQueue2 initially contains : []
Exception: java.lang.NullPointerException

参考: https: Java/util/AbstractQueue.html#addAll-E-