This post explains how to insert elements into an ArrayList in java. Two methods are available — arraylist add() and ArrayList addAll() — and each one is overloaded. Below we look at how to java add to list both single elements and whole collections.
1. add(E e)
2. add(int index, E e)
3. add(Collection c)
4. add(int index, Collection c)
Java ArrayList add(E e)
The ArrayList add() method inserts the supplied element into the ArrayList. It appends the specified element (e) to the end of the ArrayList. The result is a boolean value: it returns true when the element is added successfully, and false otherwise.
boolean add(E e);
Where E represents the type of elements in the ArrayList.
e is the element that you want to add in the ArrayList.
return type: Its return type is boolean.
import java.util.ArrayList;
public class ArraylistExample
{
public static void main(String[] args)
{
ArrayList<String> listOfNames = new ArrayList<String>();
listOfNames.add("JAVA");
listOfNames.add("GOAL");
listOfNames.add("Website");
// Adding Same element again
boolean isSuccessFullyAdded = listOfNames.add("GOAL");
System.out.println("Element added again = "+ isSuccessFullyAdded);
for(String name : listOfNames)
{
System.out.println("Names from list = "+name);
}
}
}
Output: Element added again = true
Names from list = JAVA
Names from list = GOAL
Names from list = Website
Names from list = GOAL

This shows why duplicates can appear in an ArrayList. Every element is kept at its own index value, so the list never checks whether that element already exists.
add(int index, E e)
Use this overload when you need to place a given element at a given position in the ArrayList. Imagine an ArrayList holding 4 elements and you want to insert a new element at position 2. Its return type is void. When the element is inserted at that position, the element currently sitting there is shifted along.
boolean add(int index, E e);
Where index, index at which the specified element is to be inserted.
E, represents the type of elements in ArrayList.
e, is the element which you want to add in ArrayList.
return type: Its return type is void.
throw, IndexOutOfBoundsException if index is invalid.
import java.util.ArrayList;
public class ArraylistExample
{
public static void main(String[] args)
{
ArrayList<String> listOfNames = new ArrayList<String>();
listOfNames.add("JAVA");
listOfNames.add("GOAL");
listOfNames.add("Website");
// Adding element at specified position
listOfNames.add(1, "Learning");
for(String name : listOfNames)
{
System.out.println("Names from list = "+name);
}
}
}
Output: Names from list = JAVA
Names from list = Learning
Names from list = GOAL
Names from list = Website

ArrayList addAll(Collection c)
In Java, ArrayList addAll() appends every element of the specified collection to the end of the ArrayList. It returns a boolean value, returning true once the collection has been added. If the specified collection is null, it throws a NullPointerException.
boolean addAll(Collection c);
Where, c collection containing elements to be added to this list.
throws NullPointerException, if the specified collection is null.
return type, Its return type is boolean.
import java.util.ArrayList;
public class ArrayListExample
{
public static void main(String[] args)
{
ArrayList<String> listOfNames = new ArrayList<String>();
listOfNames.add("JAVA");
listOfNames.add("GOAL");
listOfNames.add("Website");
ArrayList<String> listOfNames2 = new ArrayList<String>();
listOfNames2.add("Learning");
listOfNames2.add("Concepts");
listOfNames.addAll(listOfNames2);
for(String name : listOfNames)
{
System.out.println("Names from list = "+name);
}
}
}
Output: Names from list = JAVA
Names from list = GOAL
Names from list = Website
Names from list = Learning
Names from list = Concepts

ArrayList addAll(int index, Collection c)
The ArrayList addAll(int index, Collection c) method appends all the elements of the specified collection at a chosen position in the ArrayList. Its return type is boolean. It inserts every element of the specified collection into the ArrayList starting at that position, shifting the elements already present there.
boolean addAll(int index, Collection c);
Where index, index at which the specified element is to be inserted.
c, collection containing elements to be added to this list.
throws NullPointerException, if the specified collection is null.
return type, it’s return type is boolean.
package package1;
import java.util.ArrayList;
public class ArrayListExample
{
public static void main(String[] args)
{
ArrayList<String> listOfNames = new ArrayList<String>();
listOfNames.add("JAVA");
listOfNames.add("GOAL");
listOfNames.add("Website");
ArrayList<String> listOfNames2 = new ArrayList<String>();
listOfNames2.add("Learning");
listOfNames2.add("Concepts");
listOfNames.addAll(1, listOfNames2);
for(String name : listOfNames)
{
System.out.println("Names from list = "+name);
}
}
}
Output: Names from list = JAVA
Names from list = Learning
Names from list = Concepts
Names from list = GOAL
Names from list = Website


