coderolls Staff
coderolls Staff Hi 👋🏼, At coderolls we write tutorials about Java programming language and related technologies.

LinkedList addLast() Method

In this article, we will see the addLast() method of the LinkedList class in Java with an example.

Introduction

LinkedList class is present in the java.util package.

LinkedList internally uses doubly linkedlist for storing the variables. That’s why LinkedList is preferred when our frequent operation is insertion and deletion.

To insert the elements at the end of the LinkedList, we can use the addLast() method.

LinkedList addLast() Method

The addLast() method will insert the specified element at the end of this list.

The addLast() method is equivalent to the add() method.

The method signature for the addLast() method is given below.

1
public void addLast(E e)

The parameter e is an element to be added at the end of the List.

The return type of this method is void, which means, this method does not return anything.

I have given a simple Java program for the addLast() method below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import java.util.LinkedList;

public class LinkedListAddLast {
  public static void main(String[] args) {
    LinkedList<String> linkedList = new LinkedList<String>();
    linkedList.add("Austin");
    linkedList.add("Boston");
    linkedList.add("Atlanta");
    linkedList.add("Madison");
    linkedList.add("Columbia");
    
    System.out.println(linkedList);
    
    // add element at the end
    linkedList.addLast("Albany");
    System.out.println(linkedList);
  }
}

Output:

1
2
[Austin, Boston, Atlanta, Madison, Columbia]
[Austin, Boston, Atlanta, Madison, Columbia, Albany]

Conclusion

1
public void addLast(E e)

The addLast() method will insert the specified element at the end of this list. This method is equivalent to the add() method. —

The example Java programs given in the above tutorial can be found at this GitHub Repository.

Let me know your thoughts or suggestions on the topic discussed above in the comment section below.

Join Newsletter
Get the latest tutorials right in your inbox. We never spam!

comments powered by Disqus