The add(E element) of java.util.Collection interface is used to add the element ‘element’ to this collection. This method returns a boolean value depicting the successfulness of the operation. If the element was added, it returns true, else it returns false.
Parameters: This method accepts a mandatory parameter element of type E which is to be added to this collection.
Return Value: This method returns a boolean value depicting the successfulness of the operation. If the element was added, it returns true, else it returns false.
Exceptions: This method throws following exceptions:
- UnsupportedOperationException: if the add 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 is null and this collection does not permit null elements
- IllegalArgumentException: if some property of the element prevents it from being added to this collection
- IllegalStateException: if the element cannot be added at this time due to insertion restrictions
Below examples illustrate the Collection add() method:
Example 1: Using LinkedList Class
The list is: [Geeks, for, Geeks] The new List is: [Geeks, for, Geeks, Last, Element]
Example 2: Using ArrayDeque Class
ArrayDeque: [Welcome, To, Geeks, 4, Geeks]
Example 3: Using ArrayList Class
Number = 15 Number = 20 Number = 25
Example 4: To demonstrate NullPointer Exception
The ArrayList is: 
- Collection contains() method in Java with Examples
- Collection isEmpty() method in Java with Examples
- Collection clear() method in Java with Examples
- Collection addAll() method in Java with Examples
- Stack addAll(int, Collection) method in Java with Example
- Stack addAll(Collection) method in Java with Example
- Garbage Collection in Java
- Iterator vs Collection in Java
- Convert an Iterable to Collection in Java
- Difference between Arrays and Collection in Java
- Output of Java programs | Set 10 (Garbage Collection)
- Sorting collection of String and StringBuffer in Java
- How to prevent objects of a class from Garbage Collection in Java
- Finding minimum and maximum element of a Collection in Java
- Java Collection| Difference between Synchronized ArrayList and CopyOnWriteArrayList
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.