How to insert all the Collection Elements at Specified position in ArrayList

In this post, I will be sharing how to insert all the collection elements at the specified position in ArrayList with examples. In the last tutorial, I shared an example of the addAll(Collection c) method which is used for adding all the elements of Collection c at the end of the list. In this tutorial, we will see another variant of the addAll(int index, Collection c) method. Given below are the two examples.

1. Append Collection elements at a specified position using String elements

2. Append Collection elements at a specified position using Integer elements

Read Also: How to add elements to an ArrayList in Java

Let's dive deep into the topic:

According to Oracle docs, the below method inserts all of the elements in the specified collection into the ArrayList, starting at the specified position. The addAll(int index, Collection c) method shifts the element currently at that position and any subsequent elements to the right (increasing their indices).

Syntax:


The syntax for the method is:

 public boolean addAll(int index, Collection c)


Parameters:


This method takes two parameters:
index: index at which the first element of the specified collection is to be inserted.
c: c represents a collection containing elements to be added to the given ArrayList.

Returns:


The addAll(int index, Collection c) method returns true if the list changed as a result of the call.

Exception:


The addAll(int index, Collection c) method can throw IndexOutOfBoundsException or NullPointerException if the specified collection is null.

How to insert all the Collection Elements at the Specified position in ArrayList

1. Append Collection elements at a specified position to the String ArrayList


1. In the below example, we have created an empty String ArrayList. Added elements to it by using add(element) method.
2. Fetch the Collection elements that are to be added to the String ArrayList.
3. Add all the elements of the collection into this ArrayList at a specified position using the ArrayList.addAll(int index, Collection c) method.
4. That's it, ArrayList with all the collection elements at the specified position has been created.

 import java.util.*;
public class AddAllSpecificPositionExample {
    public static void main(String args[]) {
       // ArrayList1 
       ArrayList<String> al = new ArrayList<String>();
       al.add("Apple");
       al.add("Orange");
       al.add("Grapes");
       al.add("Mango");
       System.out.println("ArrayList1 before addAll:"+al);
       //ArrayList2 
       ArrayList<String> al2 = new ArrayList<String>();
       al2.add("Blackberry");
       al2.add("Strawberry");
       al2.add("Banana");
       al2.add("Guava");
       System.out.println("ArrayList2 content:"+al2);

       //Adding ArrayList2 in ArrayList1 at 4th position(index =3)
       al.addAll(3, al2);
       System.out.println("ArrayList1 after adding ArrayList2 at 4th Pos:\n"+al);
    }
}


Output:
ArrayList1 before addAll:[Apple, Orange, Grapes, Mango]
ArrayList2 content:[Blackberry, Strawberry, Banana, Guava]
ArrayList1 after adding ArrayList2 at 4th Pos:
[Apple, Orange, Grapes, Blackberry, Strawberry, Banana, Guava, Mango]


2. Append Collection elements at a specified position to the Integer ArrayList


1. In the below example, we have created an empty Integer ArrayList. Added elements to this ArrayList by using add(element) method.
2. Fetch the Collection elements that are to be added to the Integer ArrayList.
3. Added all the elements of the collection into the Integer ArrayList at a specified position using the ArrayList.addAll(int index, Collection c) method.

 import java.util.*;
public class AddAllSpecificPositionExample2 {
    public static void main(String args[]) {
       // ArrayList1 
       ArrayList<Integer> al = new ArrayList<>();
       al.add(111);
       al.add(222);
       al.add(333);
       al.add(444);
       System.out.println("ArrayList1 before addAll:"+al);
       //ArrayList2 
       ArrayList<Integer> al2 = new ArrayList<>();
       al2.add(555);
       al2.add(666);
       al2.add(777);
       al2.add(888);
       System.out.println("ArrayList2 content:"+al2);

       //Adding ArrayList2 in ArrayList1 at 2nd position(index =1)
       al.addAll(1, al2);
       System.out.println("ArrayList1 after adding ArrayList2 at 2nd Pos:\n"+al);
    }
}


Output:
ArrayList1 before addAll:[111, 222, 333, 444]
ArrayList2 content:[555, 666, 777, 888]
ArrayList1 after adding ArrayList2 at 2nd Pos:
[111, 555, 666, 777, 888, 222, 333, 444]


That's all for today. Please mention in the comments if you have any questions related to how to insert all the Collection elements at a specific position in ArrayList.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry