Java Collection addAll() method
The addAll() method of Java Collection Interface appends or inserts all the elements of the specified collection to this collection.
Syntax
Parameters
The parameter ‘c’ represents the elements to be added into this Collection.
Return Value
The addAll() method returns a Boolean value true, if the queue has changed as a result of this call else it returns false.
Throws
The addAll() method throws:
UnsupportedOperationException – if the addAll operation is not supported by this collection
ClassCastException- if the class of the specified element prevents it from being added to this collection.
NullPointerException- if the specified element or the specified collection is null and this collection does not allow null elements
IllegalArgumentException- if some property of the element prevents it from being added to this collection
IllegalStateException- if all the elements cannot be inserted at this time due to some insertion restrictions
Example 1
Output:
Before applying addAll() : Elements in queue : [] Elements in list : [5, 10, 15, 20, 25, 30, 35, 40, 45, 50] After applying addAll : Elements in queue : [50, 35, 20, 5, 40, 25, 10, 45, 30, 15] Elements in list : [5, 10, 15, 20, 25, 30, 35, 40, 45, 50]
Example 2
Output:
Exception in thread "main" java.lang.IllegalArgumentException at java.util.concurrent.ConcurrentLinkedQueue.addAll(ConcurrentLinkedQueue.java:526) at com.tutorAspire.JavaCollectionAddAllExample2.main(JavaCollectionAddAllExample2.java:11)
Example 3
Output:
Exception in thread "main" java.lang.NullPointerException at java.util.ArrayDeque.addLast(ArrayDeque.java:249) at java.util.ArrayDeque.add(ArrayDeque.java:423) at com.tutorAspire.JavaCollectionAddAllExample3.main(JavaCollectionAddAllExample3.java:13)