Java List add() addAll() Methods Tutorial

In this section we will learn what the List add() and addAll() methods are and how to use them in Java.

What is Java List add() Method?

The Java List add() method is used to add a new element to a list object.

When using this method, each new value will be added to the end of the target list object.

For example, if you have a list object with the values 1,2,3 and now use this method to add the value 4, then after running the method, the value 4 will be added to the list and you’ll get:

1,2,3,4

Java List add() Method Syntax:

boolean add(E e)

add() Method Parameters:

The method takes one argument, and that is the element we want to append to the target list.

add() Method Return Value:

The return value of this method is of Boolean type.

If the value was added to the target list, then the return value of this method becomes true, which means the operation was successful. Otherwise, if the method wasn’t added to the target list, the return value will be false.

add() Method Exceptions:

This method might throw any of the exceptions below:

UnsupportedOperationException – if the add operation is not supported by the target list object.

ClassCastException – This exception will be thrown if you try to add an element of type other than the data type of the target list object.

NullPointerException – If you attempt to add null value to a list object, you’ll get this exception.

IllegalArgumentException – If a property of the element that you’re adding to a list is preventing it from this operation, you’ll get this exception.

Example: using List add() method

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

public class Main {

    public static void main(String[] args){
        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(5);
        list.add(6);

    }
}

In this example, we’ve created a list object and then added 6 integer numbers via the add() method.

What is Java List addAll() Method?

The Java List addAll() method is used to add the elements of a collection (an object that inherits from the Collection interface) into a list object.

Basically, if you have a second list object and want to copy its entire elements to the current list object, this method is the way to do so.

Java List addAll () Method Syntax:

boolean addAll(Collection<? extends E> c)

addAll () Method Parameters:

The method takes one argument, and that is a reference to the list object which we want to copy all its elements and bring it into the current list object.

addAll () Method Return Value:

The return value of this method is of Boolean type.

If the operation was successful, the result will be true otherwise, you’ll get the value false.

addAll () Method Exceptions:

Just like the add() method, this method also throws the same sort of exceptions.

Check the exceptions of the add() method for more information.

Example: using List addAll () method

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

public class Main {

    public static void main(String[] args){
        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(5);
        list.add(6);

        List<Integer> list2 = new ArrayList<>();
        list2.addAll(list);

        for(int i: list2){
         System.out.println(i);
        }
    }
}

Output:

1

2

3

4

5

6

In this example, we have 2 lists. The first one has already 6 elements in its body. Now for the second list object, we’ve called the `addAll()` method and copied the entire elements of the first list object into this second one.

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies