The add(E element) method of the Java Collection interface is used to insert an element into a collection. It returns true if the element is successfully added and may throw runtime exceptions if the operation is not supported or if the element is invalid.
- Supported by most collection classes such as ArrayList, LinkedList, and ArrayDeque
- Throws exceptions like UnsupportedOperationException or NullPointerException when insertion is not allowed
Syntax
boolean add(E element)
- Parameters: "element" The element to be added to the collection.
- Return Value: Returns true if the element is added successfully and false if the addition fails.
Let us implement the add() method in all the above cases using clean Java examples as follows:
1. Using ArrayList
This Java program demonstrates how to add elements to an ArrayList using the add() method.
import java.util.*;
public class GFG {
public static void main(String[] args) {
Collection<Integer> arrlist = new ArrayList<Integer>(5);
arrlist.add(15);
arrlist.add(20);
arrlist.add(25);
for (Integer number : arrlist) {
System.out.println("Number = " + number);
}
}
}
Output
Number = 15 Number = 20 Number = 25
Explanation: It creates an ArrayList with an initial capacity, adds integers using add().
2. Using LinkedList
This Java program demonstrates how to add elements to a LinkedList using the add() method.
import java.util.*;
public class GFG {
public static void main(String args[]) {
Collection<String> list = new LinkedList<String>();
list.add("Geeks");
list.add("for");
list.add("Geeks");
System.out.println("The list is: " + list);
list.add("Last");
list.add("Element");
System.out.println("The new list is: " + list);
}
}
Output
The list is: [Geeks, for, Geeks] The new list is: [Geeks, for, Geeks, Last, Element]
Explanation:
- We create an empty LinkedList of strings.
- Use add() to insert elements.
3. Using ArrayDeque
This Java program shows how to add elements to an ArrayDeque using the add() method.
import java.util.*;
public class GFG {
public static void main(String args[]) {
Collection<String> de_que = new ArrayDeque<String>();
de_que.add("Welcome");
de_que.add("To");
de_que.add("Geeks");
de_que.add("4");
de_que.add("Geeks");
System.out.println("ArrayDeque: " + de_que);
}
}
Output
ArrayDeque: [Welcome, To, Geeks, 4, Geeks]
Explanation: It creates an ArrayDeque, adds multiple elements with add(), and prints the contents of the deque.
4. NullPointerException Example
This Java program demonstrates how adding a null element to a collection can be handled using a try-catch block.
import java.util.*;
class GFG {
public static void main(String args[]) {
Collection<String> list = new ArrayList<String>();
System.out.println("The ArrayList is: " + list);
try {
list.add(null);
} catch (Exception e) {
System.out.println("Exception: " + e);
}
}
}
Output
The ArrayList is: []
Explanation:
- The add() method successfully adds null if the collection allows null elements.
- If null is not allowed, it throws a NullPointerException. Always check if your collection supports nulls.