Collection add() method in Java with Examples

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.

Syntax:

Collection.add(E element)

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to illustrate boolean add() method
  
import java.io.*;
import java.util.*;
  
public class GFG {
    public static void main(String args[])
    {
  
        // creating an empty LinkedList
        Collection<String> list = new LinkedList<String>();
  
        // use add() method to add elements in the list
        list.add("Geeks");
        list.add("for");
        list.add("Geeks");
  
        // Output the present list
        System.out.println("The list is: " + list);
  
        // Adding new elements to the end
        list.add("Last");
        list.add("Element");
  
        // printing the new list
        System.out.println("The new List is: " + list);
    }
}

chevron_right


Output:

The list is: [Geeks, for, Geeks]
The new List is: [Geeks, for, Geeks, Last, Element]

Example 2: Using ArrayDeque Class

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to illustrate add() method
  
import java.util.*;
  
public class ArrayDequeDemo {
    public static void main(String args[])
    {
        // Creating an empty ArrayDeque
        Collection<String> de_que = new ArrayDeque<String>();
  
        // Use add() method to add elements into the Deque
        de_que.add("Welcome");
        de_que.add("To");
        de_que.add("Geeks");
        de_que.add("4");
        de_que.add("Geeks");
  
        // Displaying the ArrayDeque
        System.out.println("ArrayDeque: " + de_que);
    }
}

chevron_right


Output:

ArrayDeque: [Welcome, To, Geeks, 4, Geeks]

Example 3: Using ArrayList Class

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to illustrate add() method
  
import java.io.*;
import java.util.*;
  
public class ArrayListDemo {
    public static void main(String[] args)
    {
  
        // create an empty array list with an initial capacity
        Collection<Integer> arrlist = new ArrayList<Integer>(5);
  
        // use add() method to add elements in the list
        arrlist.add(15);
        arrlist.add(20);
        arrlist.add(25);
  
        // prints all the elements available in list
        for (Integer number : arrlist) {
            System.out.println("Number = " + number);
        }
    }
}

chevron_right


Output:

Number = 15
Number = 20
Number = 25

Example 4: To demonstrate NullPointer Exception

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to illustrate boolean add()
  
import java.util.*;
  
public class LinkedListDemo {
    public static void main(String args[])
    {
  
        // Creating an empty ArrayList
        Collection<String>
            list = new ArrayList<String>();
  
        // Displaying the list
        System.out.println("The ArrayList is: " + list);
  
        try {
            // Appending the null to the list
            list.add(null);
        }
        catch (Exception e) {
            System.out.println("Exception: " + e);
        }
    }
}

chevron_right


Output:

The ArrayList is: []

Reference: https://docs.oracle.com/javase/9/docs/api/java/util/Collection.html#add-E-



My Personal Notes arrow_drop_up

Technical Content Engineer at GeeksForGeeks

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.