How to Add Elements to LinkedList in Java Example

This example shows how to add elements to LinkedList in Java using the add method of the LinkedList class. The example also shows how to add all elements of another Collection to the LinkedList object using the addAll method.

How to add elements to the LinkedList in Java?

There are several ways using which we can add elements to the LinkedList as given below.

1. Using the add method

The add method of the LinkedList class adds the specified element to the linked list object.

The add method appends the specified element at the end of the linked list and returns true.

Output

As you can see from the output, the add method appends the specified element at the end of the linked list.

How to insert an element in the LinkedList?

What if you want to insert an element to the LinkedList at the specified position? In that case, use the overloaded add method that accepts an index at which you want to insert the element in the list.

Note: The return type of this overloaded add method is void. It inserts an element at the specified index and shifts the subsequent elements to the right by adding 1 to their indices.

Output

2. Using the addFirst and addLast methods

The addFirst and addLast methods insert the specified element at the start and end of the linked list respectively.

The return type of both of these methods is void.

Output

Please visit how to insert an element at the beginning of the LinkedList example to know more.

Note: The addLast and add methods are similar because both of them append the specified element at the end of the linked list object.

3. Using the addAll method

The addAll method of the LinkedList class adds all the elements of the specified Collection to this linked list object.

The addAll method appends all the elements of the specified collection object at the end of the linked list and returns true if this linked list object is changed after this method call.

Since the addAll method accepts the Collection, I am going to show you how to add all elements of an ArrayList to the LinkedList in below given example.

Output

As you can see from the output, all the elements of an ArrayList are appended to the LinkedList object at the end. If you want to insert all the elements of the Collection to the linked list at the specified position, use the below given overloaded addAll method.

This method inserts all the elements of the specified collection to the linked list object at the given index and shifts existing subsequent elements to the right.

Output

This example is a part of the LinkedList in Java tutorial.

Please let me know your views in the comments section below.

References:
Java 8 LinkedList

About the author

RahimV

RahimV

My name is RahimV and I have over 16 years of experience in designing and developing Java applications. Over the years I have worked with many fortune 500 companies as an eCommerce Architect. My goal is to provide high quality but simple to understand Java tutorials and examples for free. If you like my website, follow me on Facebook and Twitter.

Add Comment

Your email address will not be published. Required fields are marked *