LinkedList in Java

linked lists are a fundamental and widely used data structure that differs from arrays in the way they allocate and store data. While arrays allocate memory for all elements together in a contiguous block, linked lists allocate separate memory blocks, called nodes, for each element.This allows linked lists to be dynamically extended or reduced to accommodate varying data sets, unlike static arrays.

However, linked lists lack direct access to individual elements, requiring traversal from the head node to reach a specific item. Despite this limitation, linked lists provide flexibility and efficient insertion and removal operations, making them valuable in scenarios where dynamic data manipulation is required.

Creating a LinkedList

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

In this example, we create a LinkedList that stores elements of type String.

Adding Elements to the LinkedList

linkedList.add("red"); linkedList.add("green"); linkedList.add("blue");

We can add elements to the LinkedList using the add() method. In this case, we add three fruits to the list.

Accessing Elements in the LinkedList

String firstElement = linkedList.get(0); System.out.println("First element: " + firstElement);

We can access elements in the LinkedList using the get() method, which takes the index of the element as an argument. In this example, we retrieve the first element in the list.

Modifying Elements in the LinkedList

linkedList.set(1, "yellow");

We can modify elements in the LinkedList using the set() method, which takes the index of the element to be modified and the new value as arguments. In this case, we change the second element to "grape".

Removing Elements from the LinkedList

linkedList.remove(2);

We can remove elements from the LinkedList using the remove() method, which takes the index of the element to be removed as an argument. In this example, we remove the third element from the list.

Iterating over the LinkedList

for (String element : linkedList) { System.out.println(element); }

We can iterate over the elements of the LinkedList using a for-each loop. This allows us to access and process each element in the list.

LinkedList | Java Source Code


How to Java LinkedList

The following Java program illustrates several of the methods supported by this LinkedList collection Framework.

import java.util.*; class TestClass { public static void main (String[] args) throws java.lang.Exception { //create a new linked list object LinkedList days = new LinkedList(); // add elements to the linked list days.add("Monday"); days.add("Tuesday"); days.add("Wednesday"); days.add("Thursday"); days.addLast("Friday"); //Display all the contents of the LinkedList Iterator<String> itr=days.iterator(); while(itr.hasNext()){ System.out.println(itr.next()); } /*Add First and Last Element in linked list*/ days.addFirst("Sunday"); days.addLast("Saturday"); System.out.println("After Addition: " + days); //Insert an element in linked list days.add(0, "Days in a Week"); //add start of linked list days.add(4,"Middle"); //add in the middle of linked list days.add(9, "End"); //add in the lst of linked list System.out.println("After Insertion: " + days); //remove days.removeFirst(); days.remove(3); //remove forth element from list days.removeLast(); System.out.println("After Addition: " + days); //Remove an element from linked list days.remove("Monday"); //remove monday from list //System.out.println("Contents : " + days); // Number of items in the linked list int elements = days.size(); System.out.println("No. of elements linked list: " + elements); // Finding elements in the linked list boolean check = days.contains("Sunday"); if(check) System.out.println("Item found !!"); else System.out.println("Not in the list!! "); // update linked list or get and set a value in a linked list Object item = days.get(0); days.set(0, (String) item + "-First Day"); System.out.println("After modification : " + days); } }

Conclusion

The LinkedList class in Java is an implementation of the List interface that provides a doubly-linked list data structure. It allows efficient insertion and removal of elements at both the beginning and end of the list, making it suitable for scenarios where frequent modifications are required.