ConcurrentLinkedQueue addAll() method in Java
Last Updated :
26 Nov, 2018
The
addAll() method of
ConcurrentLinkedQueue is used to inserts all the elements of the Collection, passed as parameter to this method, at the end of a ConcurrentLinkedQueue. The insertion of element is in same order as returned by the collections iterator.
Syntax:
public boolean addAll(Collection<? extends E> c)
Parameter: This method takes a parameter
cwhich represent collection whose elements are needed to be appended at the end of this ConcurrentLinkedQueue.
Returns: This method returns
true if at least one action of insertion is performed.
Exception: This method throw two different Exceptions:
- NullPointerException - if the passed collection or any of its elements are null.
- IllegalArgumentException - if the passed collection is this queue itself.
Below programs illustrate addAll() method of ConcurrentLinkedQueue:
Example 1: Trying to add a list of numbers to ConcurrentLinkedQueue.
Java
// Java Program Demonstrate addAll()
// method of ConcurrentLinkedQueue
import java.util.concurrent.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// create an ConcurrentLinkedQueue
ConcurrentLinkedQueue<Integer>
queue = new ConcurrentLinkedQueue<Integer>();
// Add Numbers to queue
queue.add(4353);
// create a ArrayList of Numbers
ArrayList<Integer> arraylist = new ArrayList<Integer>();
// add some numbers to ArrayList
arraylist.add(377139);
arraylist.add(624378);
arraylist.add(654793);
arraylist.add(764764);
arraylist.add(838494);
arraylist.add(632845);
// Displaying the existing ConcurrentLinkedQueue
System.out.println("ConcurrentLinkedQueue: " + queue);
// Displaying the existing Collection
System.out.println("Collection to be added: " + arraylist);
// apply addAll() method and passed the arraylist as parameter
boolean response = queue.addAll(arraylist);
// Displaying the existing ConcurrentLinkedQueue
System.out.println("Collection added: " + response);
// Displaying the existing ConcurrentLinkedQueue
System.out.println("ConcurrentLinkedQueue: " + queue);
}
}
Output:
ConcurrentLinkedQueue: [4353]
Collection to be added: [377139, 624378, 654793, 764764, 838494, 632845]
Collection added: true
ConcurrentLinkedQueue: [4353, 377139, 624378, 654793, 764764, 838494, 632845]
Example 2: Trying to add a list of Strings to ConcurrentLinkedQueue.
Java
// Java Program Demonstrate addAll()
// method of ConcurrentLinkedQueue
import java.util.concurrent.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// create an ConcurrentLinkedQueue
ConcurrentLinkedQueue<String>
queue = new ConcurrentLinkedQueue<String>();
// Add String to queue
queue.add("Aman");
queue.add("Amar");
// create a ArrayList of Numbers
ArrayList<String> arraylist = new ArrayList<String>();
// add some numbers to ArrayList
arraylist.add("Sanjeet");
arraylist.add("Rabi");
arraylist.add("Debasis");
arraylist.add("Raunak");
arraylist.add("Mahesh");
// Displaying the existing ConcurrentLinkedQueue
System.out.println("ConcurrentLinkedQueue: " + queue);
// Displaying the existing Collection
System.out.println("Collection to be added: " + arraylist);
// apply addAll() method and passed the arraylist as parameter
boolean response = queue.addAll(arraylist);
// Displaying the existing ConcurrentLinkedQueue
System.out.println("Collection added: " + response);
// Displaying the existing ConcurrentLinkedQueue
System.out.println("ConcurrentLinkedQueue: " + queue);
}
}
Output:
ConcurrentLinkedQueue: [Aman, Amar]
Collection to be added: [Sanjeet, Rabi, Debasis, Raunak, Mahesh]
Collection added: true
ConcurrentLinkedQueue: [Aman, Amar, Sanjeet, Rabi, Debasis, Raunak, Mahesh]
Example 3: Showing
NullPointerException
Java
// Java Program Demonstrate addAll()
// method of ConcurrentLinkedQueue
import java.util.concurrent.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// create an ConcurrentLinkedQueue
ConcurrentLinkedQueue<String>
queue = new ConcurrentLinkedQueue<String>();
// Add String to queue
queue.add("Aman");
queue.add("Amar");
// create a ArrayList which is equal to null
ArrayList<String> arraylist = null;
// Displaying the existing ConcurrentLinkedQueue
System.out.println("ConcurrentLinkedQueue: " + queue);
// Displaying the existing Collection
System.out.println("Collection to be added: " + arraylist);
try {
// apply addAll() method and pass the arraylist as parameter
// since the arraylist is null, exception will be thrown
boolean response = queue.addAll(arraylist);
}
catch (NullPointerException e) {
System.out.println("Exception thrown while adding null: " + e);
}
}
}
Output:
ConcurrentLinkedQueue: [Aman, Amar]
Collection to be added: null
Exception thrown while adding null: java.lang.NullPointerException
Example 4: Showing IllegalArgumentException
Java
// Java Program Demonstrate addAll()
// method of ConcurrentLinkedQueue
import java.util.concurrent.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// create an ConcurrentLinkedQueue
ConcurrentLinkedQueue<String>
queue = new ConcurrentLinkedQueue<String>();
// Add String to queue
queue.add("Aman");
queue.add("Amar");
// Displaying the existing ConcurrentLinkedQueue
System.out.println("ConcurrentLinkedQueue: " + queue);
try {
// apply addAll() method and passed the queue as parameter
boolean response = queue.addAll(queue);
}
catch (IllegalArgumentException e) {
System.out.println("Exception thrown while adding queue"
+ " to itself when collection is required: " + e);
}
}
}
Output:
ConcurrentLinkedQueue: [Aman, Amar]
Exception thrown
while adding queue to itself
when collection is required: java.lang.IllegalArgumentException
Reference: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentLinkedQueue.html#addAll-java.util.Collection-
Similar Reads
ConcurrentLinkedQueue add() method in Java
The add() method of ConcurrentLinkedQueue is used to insert the element, passed as parameter to add() of ConcurrentLinkedQueue, at the tail of this ConcurrentLinkedQueue. This method returns True if insertion is successful. ConcurrentLinkedQueue is unbounded, so this method will never throw IllegalS
2 min read
ConcurrentLinkedDeque add() method in Java
The java.util.concurrent.ConcurrentLinkedDeque.add() is an in-built function in Java which inserts the specified element at the end of the deque. Syntax: conn_linked_deque.add(elem) Parameter: The method accepts only a single parameter elem which is to be added to tail of the ConcurentLinkedDeque. R
2 min read
ConcurrentLinkedDeque addLast() method in Java
The java.util.concurrent.ConcurrentLinkedDeque.addLast() is an in-built function in Java which inserts the specified element to the end of the deque. Syntax: conn_linked_deque.addLast(elem) Parameter: The method accepts only a single parameter elem which is to be added to the end of the ConcurrentLi
2 min read
ConcurrentLinkedDeque addFirst() method in Java
The java.util.concurrent.ConcurrentLinkedDeque.addFirst() is an in-built function in Java which inserts the specified element at the front of the ConcurrentLinkedDeque. Syntax: conn_linked_deque.addFirst(elem) Parameter: The method accepts only a single parameter elem which is to be added to the beg
2 min read
ConcurrentLinkedQueue size() method in Java
The size() method of ConcurrentLinkedQueue is used to returns number of elements that this ConcurrentLinkedQueue contains. Syntax: public int size() Returns: This method number of elements in this ConcurrentLinkedQueue. Below programs illustrate size() method of ConcurrentLinkedQueue: Example 1: Jav
2 min read
ConcurrentLinkedQueue poll() method in Java
The poll() method of ConcurrentLinkedQueue is used to remove and return the head of this ConcurrentLinkedQueue. If the ConcurrentLinkedQueue is empty then this method will return null. Syntax: public E poll() Returns: This method remove and returns the head of this ConcurrentLinkedQueue or null if t
2 min read
ConcurrentLinkedQueue peek() method in Java
The peek() method of ConcurrentLinkedQueue is used to return the head of the ConcurrentLinkedQueue. It retrieves but does not remove, the head of this ConcurrentLinkedQueue. If the ConcurrentLinkedQueue is empty then this method returns null. Syntax: public E peek() Returns: This method returns the
2 min read
ConcurrentLinkedQueue offer() method in Java
The offer() method of ConcurrentLinkedQueue is used to insert the element, passed as parameter, at the tail of this ConcurrentLinkedQueue. This method returns True if insertion is successful. ConcurrentLinkedQueue is unbounded, so this method offer() will never returns false. Syntax: public boolean
2 min read
ConcurrentLinkedDeque clear() method in Java
The java.util.concurrent.ConcurrentLinkedDeque.clear() method is an inbuilt method in Java which removes the elements in the Deque. Syntax: public void clear() Parameters: The method do not accepts any parameter. Return Value: The function does not return anything Below programs illustrate the Concu
2 min read
ConcurrentLinkedDeque addAll() method in Java with Examples
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
3 min read