Java

LinkedList in Java

Java provides a LinkedList class which is nothing but an ordered collection that holds multiple objects of the same data type. It belongs to the collection framework of java.util package and it extends/inherits the AbstractList class. The LinkedList class provides numerous methods to perform different functionalities e.g. add() method, remove() method, and many more.

This write-up presents a comprehensive guide for the following concepts regarding Java LinkedList:

So, let’s get started!

What is a LinkedList

The java LinkedList stores the data in a sequence of a container and each container holds the address of its adjacent container. In order to store the data, the java LinkedList utilizes the concept of doubly LinkedList. The java LinkedLists maintain the elements/items by their order of insertion and it may or may not have duplicate items.

How to Work with LinkedList

We have to follow the below-given steps to work with LinkedList:

Firstly, we have to import the LinkedList class from java.util package as shown in the below-given snippet:

import java.util.LinkedList;

The basic syntax to create an object of the LinkedList class will be:

LinkedList<String> flowers= new LinkedList<String>();

The above snippet creates a String type object of the LinkedList class named “flowers”.

The object will be created on the basis of the data type you want to work with. For example, in the above snippet, we create a string-type object because we want to store the string values, and similarly, you can use different data types depending on your needs.

For instance, if you want to store integer type values then you can create the object of LinkedList as

LinkedList<Integer> flowers= new LinkedList<Integer>();

Similarly, you can create the object of other data types as well but must remember that you have to provide the equivalent wrapper class of all these data types e.g. Integer for int, Double for double, and so on.

How to Add Items in LinkedList

The LinkedList class provides many methods that can be used to achieve different functionalities. For instance, the add() method can be used to add new items in a LinkedList.

Example

In the below snippet, we import the LinkedList class, and afterward, we create the object of the LinkedList class. Finally, we add different items to the list by using the object of the LinkedList class:

import java.util.LinkedList;

public class LinkedListExamples {

public static void main(String[] args) {

LinkedList<String> flowers = new LinkedList<>();

flowers.add("Rose");

flowers.add("Sunflower");

flowers.add("Tulip");

flowers.add("Jasmine");

flowers.add("Daisy");

System.out.println(flowers);

}

}

The below screenshot shows the complete code along with its output:

The output authenticates the working of the add() method as it succeeds in adding the items to the list.

The addFirst() and addLast() methods can be utilized to add the items at first and last index in a list respectively. Below given snippet elaborates a proper way of utilizing these methods:

The output authenticates the working of both these methods.

How to Access LinkedList Items

The get() method can be used to access the individual item of the list based on the index number. The getFirst() and getLast() methods can be used to modify the first and last items of the list respectively.

Example

The below code will show how to access an item of the LinkedList:

System.out.println(flowers.get(2));

Here, we utilize the get() method with the object of the LinkedList class and inside the get method, we specify a value 2 to access the value present at the second index of the list.

In LinkedList, indexing starts from the 0th index, and in this example, we try to access the 2nd index of the list which holds a value “Tulip”. So, the output verifies that the get() method accesses the appropriate value.

How to Remove Individual Item from LinkedList

The remove() method can be used to delete the unnecessary items from the list.

Example

Let’s consider we want to remove the third item of the LinkedList then all we have to do is just pass the index number of that item to the remove() method and the rest of the work will be done by the remove() method:

flowers.remove(2);

The code and its respective output are shown in the below-given screenshot:

The output authenticates the working of the remove() method as it succeeds in deleting the third item.

In LinkedLists the removeFirst() and removeLast() methods can be used to remove the first and last item from a list respectively. The below snippet provides the practical implementation of these methods along with respective output:

The output shows only three items(first and last items excluded) of the list which affirm that both these methods are working effectively.

How to Clear a LinkedList

The LinkedList class provides a clear() method to remove all the items from the list. Consider the below-given code for a better understanding of the clear() method.

flowers.clear();

The output of the clear method is shown in the following screenshot:

Output verifies the working of the clear() method as it clears all the data of the list.

How to Modify Items of LinkedList

In order to change/modify the items of the list we can use the set() method of the LinkedList class.

Example

Let’s consider we want to change the values of the 3rd index from “Jasmine” to “Sweet Jasmine” then we can do it in this way:

flowers.set(3, "Sweet Jasmine");

The complete code and its respective output are provided in the following screenshot:

The output authenticates the working of the set() method as it successfully modifies the value of the third index.

How to Find the Size of LinkedList

The size() method of the LinkedList class can be used to find the size of the list.

Example

The below-given snippet shows how the size() method works in java:

flowers.size();

The entire coding snippet and respective output are shown in the following screenshot:

The output verifies the appropriateness of the size() method.

How to Iterate Through LinkedList

We can iterate through a LinkedList by means of for-loop, for-each loop, for-each method, etc.

Example

In this example we will utilize one for loop with the get() method to iterate through the list:

for (int i = 0; i < flowers.size(); i++) {

System.out.println(flowers.get(i));

}

The below-given screenshot will provide a detailed picture of how to iterate through a list using a for loop:

The output authenticates the working of the for loop.

How to Sort a LinkedList

java.util provides a very useful class named Collections which provides numerous methods that can be used to perform different functionalities. For instance, we can sort the LinkedList alphabetically or numerically with the help of the sort() method of the Collection class.

Example

In the below code snippet, we utilize the sort() method to sort the flowers in alphabetical order:

Collections.sort(flowers);

The below-given snippet shows the complete code and its respective output:

The output verifies that the sort() method provides an alphabetically sorted list.

Conclusion

In Java, the LinkedList can be used by simply importing the LinkedList class of java.util package in our project. Multiple methods are available in Java to work with LinkedList like add(), remove(), set(), methods can be used to add, delete, and modify the LinkedList items. The LinkedList class provides some methods to target the first and last index of the list e.g. addFirst, getLast, etc. This write-up explains the concept of LinkedList in Java and for profound understanding screenshots are provided with each example.

About the author

Anees Asghar

I am a self-motivated IT professional having more than one year of industry experience in technical writing. I am passionate about writing on the topics related to web development.