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: 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 5 following exceptions listed below as follows:
- 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
Now we will be implementing this method over different classes as it is a very important and essential method when it comes downs to java programming so here we will be stressing over each class as follows:
- LinkedList class
- ArrayDeque
- ArrayList class
- NullPointerException is Thrown
Let us implement add() method in all 4 above listed cases via clean java examples as follows:
Example 1: LinkedList Class
Java
import java.io.*;
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]
Example 2: ArrayDeque Class
Java
import java.util.*;
public class ArrayDequeDemo {
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]
Example 3: Using ArrayList Class
Java
import java.io.*;
import java.util.*;
public class ArrayListDemo {
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
Geeks do keep an bound over special case where NullPointer Exception will be thrown as show in below example as follows:
Example 4:
Java
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: []
Output explanation: Here we need to pick it up as we will only receive a List. So it is good practice to document for add() method either it is accepting it whether it needs to support null.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
07 Oct, 2022
Like Article
Save Article