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.
import java.util.concurrent.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
ConcurrentLinkedQueue<Integer>
queue = new ConcurrentLinkedQueue<Integer>();
queue.add( 4353 );
ArrayList<Integer> arraylist = new ArrayList<Integer>();
arraylist.add( 377139 );
arraylist.add( 624378 );
arraylist.add( 654793 );
arraylist.add( 764764 );
arraylist.add( 838494 );
arraylist.add( 632845 );
System.out.println( "ConcurrentLinkedQueue: " + queue);
System.out.println( "Collection to be added: " + arraylist);
boolean response = queue.addAll(arraylist);
System.out.println( "Collection added: " + response);
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.
import java.util.concurrent.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
ConcurrentLinkedQueue<String>
queue = new ConcurrentLinkedQueue<String>();
queue.add( "Aman" );
queue.add( "Amar" );
ArrayList<String> arraylist = new ArrayList<String>();
arraylist.add( "Sanjeet" );
arraylist.add( "Rabi" );
arraylist.add( "Debasis" );
arraylist.add( "Raunak" );
arraylist.add( "Mahesh" );
System.out.println( "ConcurrentLinkedQueue: " + queue);
System.out.println( "Collection to be added: " + arraylist);
boolean response = queue.addAll(arraylist);
System.out.println( "Collection added: " + response);
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
import java.util.concurrent.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
ConcurrentLinkedQueue<String>
queue = new ConcurrentLinkedQueue<String>();
queue.add( "Aman" );
queue.add( "Amar" );
ArrayList<String> arraylist = null ;
System.out.println( "ConcurrentLinkedQueue: " + queue);
System.out.println( "Collection to be added: " + arraylist);
try {
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
import java.util.concurrent.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
ConcurrentLinkedQueue<String>
queue = new ConcurrentLinkedQueue<String>();
queue.add( "Aman" );
queue.add( "Amar" );
System.out.println( "ConcurrentLinkedQueue: " + queue);
try {
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-
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...