Java LinkedList add() addAll() addFirst() addLast() Tutorial

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

What is Java LinkedList add() Method?

The Java LinkedList add() method is used to add an element to the end (tail) of a LinkedList object.

Note: the method has another variant that allows us to add an element to a specific index of the list if needed.

Java add() Method Syntax:

public boolean add(E e)

public void add(int index, E element)

add() Method Parameters:

The method has two variants:

On the first variant: it takes only one argument and that is the element we want to add to the end (tail) of a list.

On the other variant, the method takes two arguments:

The first argument is the index number, which is the position we want to add the new element.

The second argument is the element itself to be added.

Note: for the second variant if there’s an element already in the specified index, that element and any other elements afterward (if any) will be pushed one level to the end of the list so that the new element could be added without removing any element that is already in the target list.

add() Method Return Value:

For the first variant of this method (the one that takes only one argument) the return value of this method is of type Boolean. If the argument was added to the list, the return value of this method will be true. Otherwise, the value false will return instead.

For the second variant of the method, the return value is void.

add() Method Exceptions:

For the second variant of this method (the one with two arguments) this method might throw one exception and that is:

IndexOutOfBoundsException: We get this exception if the index number (the first argument) is out of range in the target list object.

Example: using LinkedList add() method

import java.util.LinkedList;
class Main{
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        list.add("John");
        list.add("Omid");
        list.add("Jack");
        list.add("Ellen");
        list.add("Barack");
        list.add("Ian");
        list.add("Paul");
        
        list.add(2, "Elon");

        for (String s: list){
            System.out.println(s);
        }
    }
}

Output:

John

Omid

Elon

Jack

Ellen

Barack

Ian

Paul

What is Java LinkedList addAll() Method?

The Java LinkedList addAll() method is used to add (copy) the entire elements of another collection object to a LinkedList object.

Java addAll() Method Syntax:

public boolean addAll(Collection<? extends E> c)

public boolean addAll(int index, Collection<? extends E> c)

addAll() Method Parameters:

The method has two variants:

On the first variant, it takes only one argument and that is a reference to a collection that we want to copy its entire element and pass them to a LinkedList object. Note that these elements will be appended to the end of a list object.

On the second variant, the method takes two arguments:

  • The first argument is the index number where we want the new elements to be added from there. Note that just like the add() method, no current element in the LinkedList will be removed.
  • The second argument is a reference to a collection object.

addAll() Method Return Value:

Both variants of this method return a value of boolean type.

If a new element was added to the target LinkedList because of calling this method, the return value will be true.

Otherwise, the value false will return instead.

addAll() Method Exceptions:

The method might throw two types of exception:

NullPointerException: we get this exception if the value for the reference collection is a null.

IndexOutOfBoundsException: we get this exception if the index number (the first argument) is out of range in the target LinkedList object.

Example: using LinkedList addAll() method

import java.util.LinkedList;
import java.util.ArrayList; 
class Main{
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        list.add("John");
        list.add("Omid");
        list.add("Jack");
        list.add("Ellen");
        list.add(2, "Elon");

        ArrayList<String> array = new ArrayList<>();
        array.add("Barack");
        array.add("Ian");
        array.add("Paul");

        list.addAll(array);

        for (String s: list){
            System.out.println(s);
        }
    }
}

Output:

John

Omid

Elon

Jack

Ellen

Barack

Ian

Paul

What is Java LinkedList addFirst() Method?

The Java LinkedList addFirst() method is used to add a new element to the beginning (head) of a LinkedList object.

Java addFirst() Method Syntax:

public void addFirst(E e)

addFirst() Method Parameters:

The method takes one argument and that is the element we want to add to the head of the target LinkedList object.

addFirst() Method Return Value:

The return value of this method is void.

addFirst() Method Exceptions:

The method does not throw an exception.

Example: using LinkedList addFirst() method

import java.util.LinkedList; 
class Main{
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        list.addFirst("John");
        list.addFirst("Omid");
        list.addFirst("Jack");
        list.addFirst("Ellen");
        list.addFirst("Elon");

        for (String s: list){
            System.out.println(s);
        }
    }
}

Output:

Elon

Ellen

Jack

Omid

John

What is Java LinkedList addLast() Method?

The LinkedList addLast() method is used to add a new element to the end (tail) of a LinkedList object.

Note: this method works the same as the add() method.

Java addLast() Method Syntax:

public void addLast(E e)

addLast() Method Parameters:

The method takes one argument and that is the element we want to add to the end (tail) of a LinkedList object.

addLast() Method Return Value:

The return value of this method is void.

addLast() Method Exceptions:

The method does not throw an exception.

Example: using LinkedList addLast() method

import java.util.LinkedList; 
class Main{
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        list.addLast("John");
        list.addLast("Omid");
        list.addLast("Jack");
        list.addLast("Ellen");
        list.addLast("Elon");

        for (String s: list){
            System.out.println(s);
        }
    }
}

Output:

John

Omid

Jack

Ellen

Elon

Leave a Reply