Java LinkedList
The LinkedList class is part of the Java Collections Framework and is
available in the java.util package. It implements the List interface,
allowing developers to store ordered collections of elements similar to an
ArrayList.
However, unlike ArrayList, a LinkedList uses a linked node structure to
store data. This structure makes LinkedList particularly efficient for
operations that involve frequent insertion or deletion of
elements.
Because LinkedList implements the List interface, it supports many of the
same methods used in ArrayList, such as add(), remove(), get(), and
clear().
Creating a LinkedList in Java
To use a LinkedList, you must first import it from the java.util
package.
Example: Creating a LinkedList
import java.util.LinkedList;
public class PlaylistExample {
public static void main(String[] args) {
// Creating a LinkedList to store song
titles
LinkedList<String> playlist = new
LinkedList<>();
playlist.add("Shape of You");
playlist.add("Blinding Lights");
playlist.add("Levitating");
playlist.add("Peaches");
System.out.println("Music Playlist: " +
playlist);
}
}
This example demonstrates how a LinkedList can be used to maintain a
playlist of songs.
How LinkedList Works
To understand LinkedList better, it helps to compare it with
ArrayList.
How ArrayList Works
An ArrayList internally uses a dynamic array to store elements. When the
array becomes full, Java creates a new larger array and copies the
existing elements into it. This structure allows fast access to elements
using indexes, but inserting or deleting elements may require shifting
many elements in the array.
How LinkedList Works
A LinkedList stores elements in nodes (containers).
Each node contains:
- The data element
- A reference to the next node
- A reference to the previous node (in doubly linked lists)
This structure creates a chain of nodes, where each node points to
the next element in the list.
Because elements are linked rather than stored in contiguous memory,
LinkedList is efficient for adding or removing elements in the middle
of the list.
ArrayList vs LinkedList
Although both classes implement the List interface, their internal
structures and performance
characteristics differ.
Table
Feature
Internal Structure
ArrayList
Dynamic array
LinkedList
Doubly linked nodes
Access Speed
Insert/Delete
Memory Usage
Faster random access
Slower for large lists
Lower
Slower random access
Faster for frequent
modifications
Slightly higher
When to Use Each
- Use ArrayList when you frequently access elements by index.
- Use LinkedList when you frequently insert or remove elements.
Common LinkedList Methods
The LinkedList class provides several methods that make it easy to
manipulate elements at the beginning or end of the list.
Table
Method Description
addFirst() Inserts an element at the beginning of the
list
addLast() Inserts an element at the end of the list
removeFirst() Removes the first element
removeLast() Removes the last element
getFirst() Retrieves the first element
getLast() Retrieves the last element
Example: Using LinkedList-Specific Methods
import java.util.LinkedList;
public class QueueSimulation {
public static void main(String[] args)
{
LinkedList<String> queue = new
LinkedList<>();
// Adding elements
queue.addFirst("Customer
A");
queue.addLast("Customer
B");
queue.addLast("Customer
C");
System.out.println("Queue: " +
queue);
// Access first and last
elements
System.out.println("First Customer: "
+ queue.getFirst());
System.out.println("Last Customer: "
+ queue.getLast());
// Removing elements
queue.removeFirst();
queue.removeLast();
System.out.println("Updated Queue: "
+ queue);
}
}
This example simulates a simple service queue, where customers
are added and removed efficiently.
The var Keyword (Java 10+)
Starting with Java 10, the var keyword allows developers to
declare variables without explicitly repeating the
type.
Example
import java.util.LinkedList;
public class VarExample {
public static void main(String[] args)
{
var cities = new
LinkedList<String>();
cities.add("Tokyo");
cities.add("Paris");
cities.add("Sydney");
System.out.println(cities);
}
}
Although var makes the code shorter, many developers still prefer
explicit type declarations for better readability.
Using the List Interface with LinkedList
In professional Java development, it is common to declare
collections using the List interface rather than the concrete
class.
Example
import java.util.List;
import java.util.LinkedList;
public class InterfaceLinkedListExample {
public static void main(String[] args)
{
List<String> languages = new
LinkedList<>();
languages.add("Java");
languages.add("Python");
languages.add("JavaScript");
System.out.println("Programming
Languages: " + languages);
}
}