Open In App

ConcurrentLinkedDeque addAll() method in Java with Examples

Last Updated : 17 Dec, 2018
Comments
Improve
Suggest changes
Like Article
Like
Report
The addAll(Collection col) of ConcurrentLinkedDeque which takes col as a parameter, where col is a Collection of elements (List, ArrayList, LinkedList etc). This entire Collection gets appended or added to the end of the Dequeue. This method just like add() method returns true if the Collection gets appended to the Deque. Syntax:
public boolean addAll(Collection c)
Parameters: This function accepts a parameter col that is the Collection of elements to be added. Return Value: This method returns a boolean value stating if the Collection gets appended to the Deque. Exception: This method accepts two exceptions:
  • NullPointerException: if any of its elements present in the collection is null or the collection is null.
  • IllegalArgumentException: if the passed collection is existing deque itself.
Below examples illustrate the addAll() method: Example 1: Adding Collection of Numbers to the first Collection of ConcurrentLinkedDeque Java
// Java program to illustrate addAll() method

import java.util.concurrent.ConcurrentLinkedDeque;

public class Example1 {
    public static void main(String[] args)
    {

        // create object of ConcurrentLinkedDeque
        ConcurrentLinkedDeque<Integer> ld1
            = new ConcurrentLinkedDeque<Integer>();

        // Add elements
        ld1.add(1);
        ld1.add(2);
        ld1.add(3);
        ld1.add(4);

        // print ld1
        System.out.println("LD1: " + ld1);

        // create object of ConcurrentLinkedDeque
        ConcurrentLinkedDeque<Integer> ld2
            = new ConcurrentLinkedDeque<Integer>();

        ld2.add(8);
        ld2.add(1);
        ld2.add(3);
        ld2.add(5);

        // print ld2
        System.out.println("LD2: " + ld2);

        // Adding elements of ld2 to ld1 at its end.
        ld1.addAll(ld2);

        System.out.println("After adding the ld2 to ld1: "
                           + ld1);
    }
}
Output:
LD1: [1, 2, 3, 4]
LD2: [8, 1, 3, 5]
After adding the ld2 to ld1: [1, 2, 3, 4, 8, 1, 3, 5]
Example 2: Showing IllegalArgumentException Java
// Java program to illustrate addAll() method

import java.util.concurrent.ConcurrentLinkedDeque;

public class Example2 {
    public static void main(String[] args)
    {
        // create object of ConcurrentLinkedDeque
        ConcurrentLinkedDeque<String> ld1
            = new ConcurrentLinkedDeque<String>();

        // Add elements
        ld1.add("John");
        ld1.add("Johnathan");
        ld1.add("Jones");
        ld1.add("James");

        // print ld1
        System.out.println("LD1: " + ld1);

        try {
            ld1.addAll(ld1);
        }
        catch (IllegalArgumentException e) {
            System.out.println("Exception thrown while "
                               + "adding deque to "
                               + "itself is: "
                               + e);
        }
    }
}
Output:
LD1: [John, Johnathan, Jones, James]
Exception thrown while adding deque to itself is: java.lang.IllegalArgumentException
Example 3: Showing NullPointerException Java
// Java program to illustrate addAll() method

import java.util.concurrent.ConcurrentLinkedDeque;

public class Example3 {
    public static void main(String[] args)
    {

        // create object of ConcurrentLinkedDeque
        ConcurrentLinkedDeque<String> ld1
            = new ConcurrentLinkedDeque<String>();

        // Add elements
        ld1.add("John");
        ld1.add("Johnathan");
        ld1.add("Jones");
        ld1.add("James");

        // print ld1
        System.out.println("LD1: " + ld1);

        // create object of ConcurrentLinkedDeque
        ConcurrentLinkedDeque<String> ld2 = null;

        // print ld2
        System.out.println("LD2: " + ld2);

        try {

            System.out.println("After adding the ld2 to ld1: ");
            ld2.addAll(ld1);
        }
        catch (NullPointerException e) {
            System.out.println(e);
        }
    }
}
Output:
LD1: [John, Johnathan, Jones, James]
LD2: null
After adding the ld2 to ld1: 
java.lang.NullPointerException
Reference: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ConcurrentLinkedDeque.html

Next Article

Similar Reads