Home » Java Collections addAll() Method with Examples

Java Collections addAll() Method with Examples

by Online Tutorials Library

Java Collections addAll() Method

The addAll() is a method of Java Collections class which adds all of the specified elements to the specified collection. The elements to be added may be specified individually or as an array.

Syntax

Following is the declaration of addAll() method:

Parameter

Parameter Description Required/Optional
c It is a collections into which elements are to be inserted. Required
elements Elements are the elements to be inserted into c. Required

Returns

The addAll() method returns true if the collection changed as a result of the method call.

Exceptions

UnsupportedOperationException– If collection c does not support the add operation.

NullPointerException– If elements contain null values and c does not permit null elements, or if c or elements are null.

IllegalArgumentException– If some properties of a value in elements prevent it from being added to c.

Compatibility Version

Java 1.5 and above

Example 1

Test it Now

Output:

Boolean Result: true  Collection Value: [1, 2, 3, 4, 5]  

Example 2

Test it Now

Output:

The List are: [Rahul, Karthik, OM, Shiva, Anand, Prem]  Boolean Result: true  Collection Value: [Rahul, Karthik, OM, Shiva, Anand, Prem, Rahul, OM, Prem]  

Example 3

Test it Now

Output:

Initial collection value: [TutorAspire, SSSIT.COM, Hindi100.COM]  Boolean Result: true  Final collection value: [TutorAspire, SSSIT.COM, Hindi100.COM, Rank1, Rank2, Rank3]  

You may also like