CoderDS
CoderDS

Reputation: 133

What is the difference between Collection.add() method and Collection.addAll() method in Collections in Java

I have two ArrayLists (a1 and a2) as below:

ArrayList a1 = new ArrayList();
a1.add(8);
a1.add("a1");

ArrayList a2 = new ArrayList();
a2.add(a1); //output : [[8, a1]]
a2.addAll(a1); //output : [[8, a1], 8, a1]

My Questions:

  1. Why does a2.addAll() method prints the data twice?
  2. What is the exact difference between add and addAll? Both the methods return boolean.

Upvotes: 6

Views: 19121

Answers (5)

durgasankar mishra
durgasankar mishra

Reputation: 1

package others;

import java.util.ArrayList; import java.util.List;

public class Practise {

public static void main(String[] args) {

//difference between add and addall 

    List<Integer> arrList = new ArrayList<Integer>(2);
    arrList.add(5);
    arrList.add(10);
    System.out.println("printing arrayLIst 1");

    for(Integer printNumber: arrList) {
        System.out.println(printNumber);
    }

    List<Integer> arr2List = new ArrayList<Integer>();
    arr2List.add(2);
    arr2List.add(4);
    System.out.println("printing arrayLIst 2");

    for(Integer printNumber:arr2List) {
        System.out.println(printNumber);
    }

    arrList.addAll(arr2List);
    System.out.println("printing updated arrayLIst 1");

    for(Integer printNumber: arrList) {
        System.out.println(printNumber);
    }   
}

}

output

printing arrayLIst 1

5 10

printing arrayLIst 2

2 4

printing updated arrayLIst 1

5 10 2 4

Upvotes: 0

Sergey Prokofiev
Sergey Prokofiev

Reputation: 1885

add is used when you need to add single element into collection.
addAll is used when you want to add all elements from source collection to your collection.

In this particular case you're using ArrayList without specifying a generic argument, so ArrayList<Object> is assumed. When you're adding a1 using add it will treat a1 as a single object.

By default, ArrayList will be printed in square brackets, so you see [[8, a1]]. After that you're using addAll so, you just copy all elements from a1, and the result is expected again.

Hope it helps!

Upvotes: 0

Ori Marko
Ori Marko

Reputation: 58772

add() just adds the object to the next index:

public boolean add(E arg0) {
    this.ensureCapacityInternal(this.size + 1);
    this.elementData[this.size++] = arg0;
    return true;
}

addAll() copies the values to the given Collection:

public boolean addAll(Collection<? extends E> arg0) {
        Object[] arg1 = arg0.toArray();
        int arg2 = arg1.length;
        this.ensureCapacityInternal(this.size + arg2);
        System.arraycopy(arg1, 0, this.elementData, this.size, arg2);
        this.size += arg2;
        return arg2 != 0;
    }

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726579

Why a2.addAll() method prints the data twice?

Because the first copy is already there after you called add(a1) on the previous line.

What is the exact difference between add() and addAll()? Both the methods return boolean.

add adds a single item, while addAll adds each item from the collection one by one. In the end, both methods return true if the collection has been modified. In case of ArrayList this is trivial, because the collection is always modified, but other collections, such as Set, may return false if items being added are already there.

Note: Part of the confusion is that your lists are untyped, so they contain a mixture of objects: a1 mixes strings and numbers, while a2 mixes strings, numbers, and lists. Using a specific generic type for your collection would prevent this confusion by letting you do either add or addAll, but not both:

List<String> a1 = new ArrayList<>();
a1.add("8");
a1.add("a1");
List<List<String>> a2 = new ArrayList<>();
a2.add(a1);
a2.addAll(a1); // <<== Does not compile
List<String> a3 = new ArrayList<>();
a3.add(a1);    // <<== Does not compile
a3.addAll(a1);

Upvotes: 7

MarthyM
MarthyM

Reputation: 1849

The method add() adds one item to your ArrayList, in this case another ArrayList.
The method addAll() adds all items from one ArrayList to another.

To illustrate:

ArrayList a1 = new ArrayList();
a1.add(8);
a1.add("a1String"); // I changed the string value to be more clear what is a1 and what is the string "a1"
// a1 content: [8, "a1String"]

ArrayList a2 = new ArrayList();
a2.add(a1);
// a2 content: [[8, "a1String"]] - basically [a1]
// a2 has one item - a1

ArrayList a3 = new ArrayList();
a3.addAll(a1);
// a3 content: [8, "a1String"] - same content as a1
// a3 has all the items from a1

Upvotes: 7

Related Questions