Java Collection addAll() methodThe addAll() method of Java Collection Interface appends or inserts all the elements of the specified collection to this collection. SyntaxParametersThe parameter 'c' represents the elements to be added into this Collection. Return ValueThe addAll() method returns a Boolean value true, if the queue has changed as a result of this call else it returns false. ThrowsThe 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 1Test it NowOutput: 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 2Test it NowOutput: Exception in thread "main" java.lang.IllegalArgumentException at java.util.concurrent.ConcurrentLinkedQueue.addAll(ConcurrentLinkedQueue.java:526) at com.javaTpoint.JavaCollectionAddAllExample2.main(JavaCollectionAddAllExample2.java:11) Example 3Test it NowOutput: 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.javaTpoint.JavaCollectionAddAllExample3.main(JavaCollectionAddAllExample3.java:13) Next TopicJava Collection |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India