Java LinkedList
A linked list is a common basic data structure that is a linear collection of data elements, whose order is not given by their physical placement in memory. Instead, each element points to the next.
Linked lists can be divided into singly linked lists and doubly linked lists.
A singly linked list contains two values: the current node's value and a reference to the next node.
A doubly linked list contains three values: the value, a reference to the previous node, and a reference to the next node.
Java LinkedList is similar to ArrayList and is a commonly used data container.
Compared to ArrayList, LinkedList has higher efficiency for adding and removing operations but lower efficiency for search and modification operations.
Use ArrayList in the following cases:
- Frequent access to a specific element in the list.
- Only adding and removing elements at the end of the list.
Use LinkedList in the following cases:
- Iterating through some elements in the list.
- Frequent adding and removing operations at the beginning, middle, and end of the list.
LinkedList inherits from the AbstractSequentialList class.
LinkedList implements the Queue interface and can be used as a queue.
LinkedList implements the List interface and can perform list-related operations.
LinkedList implements the Deque interface and can be used as a deque.
LinkedList implements the Cloneable interface and can be cloned.
LinkedList implements the java.io.Serializable interface and supports serialization for transmission.
The LinkedList class is located in the java.util package and needs to be imported before use. The syntax is as follows:
// Import LinkedList class
import java.util.LinkedList;
LinkedList<E> list = new LinkedList<E>(); // General creation method
or
LinkedList<E> list = new LinkedList(Collection<? extends E> c); // Create a linked list using a collection
Creating a simple linked list example:
Example
import java.util.LinkedList;
public class tutorialproTest {
public static void main(String[] args) {
LinkedList<String> sites = new LinkedList<String>();
sites.add("Google");
sites.add("tutorialpro");
sites.add("Taobao");
sites.add("Weibo");
System.out.println(sites);
}
}
The output of the above example is:
[Google, tutorialpro, Taobao, Weibo]
In most cases, using ArrayList to access random elements in the list is more efficient, but LinkedList provides more efficient methods in the following scenarios.
Adding elements at the beginning of the list:
Example
// Import LinkedList class
import java.util.LinkedList;
public class tutorialproTest {
public static void main(String[] args) {
LinkedList<String> sites = new LinkedList<String>();
sites.add("Google");
sites.add("tutorialpro");
sites.add("Taobao");
// Use addFirst() to add elements at the head
sites.addFirst("Wiki");
System.out.println(sites);
}
}
The output of the above example is:
[Wiki, Google, tutorialpro, Taobao]
Adding elements at the end of the list:
Example
// Import LinkedList class
import java.util.LinkedList;
public class tutorialproTest {
public static void main(String[] args) {
LinkedList<String> sites = new LinkedList<String>();
sites.add("Google");
sites.add("tutorialpro");
sites.add("Taobao");
// Use addLast() to add elements at the tail
sites.addLast("Weibo");
System.out.println(sites);
}
}
The output of the above example is:
[Google, tutorialpro, Taobao, Weibo]
LinkedList<String> sites = new LinkedList<String>();
sites.add("Google");
sites.add("tutorialpro");
sites.add("Taobao");
// Using addLast() to add elements at the end
sites.addLast("Wiki");
System.out.println(sites);
}
}
The output of the above example is:
[Google, tutorialpro, Taobao, Wiki]
Removing an element from the beginning of the list:
Example
// Importing LinkedList class
import java.util.LinkedList;
public class tutorialproTest {
public static void main(String[] args) {
LinkedList<String> sites = new LinkedList<String>();
sites.add("Google");
sites.add("tutorialpro");
sites.add("Taobao");
sites.add("Weibo");
// Using removeFirst() to remove the head element
sites.removeFirst();
System.out.println(sites);
}
}
The output of the above example is:
Removing an element from the end of the list:
Example
// Importing LinkedList class
import java.util.LinkedList;
public class tutorialproTest {
public static void main(String[] args) {
LinkedList<String> sites = new LinkedList<String>();
sites.add("Google");
sites.add("tutorialpro");
sites.add("Taobao");
sites.add("Weibo");
// Using removeLast() to remove the tail element
sites.removeLast();
System.out.println(sites);
}
}
The output of the above example is:
Getting the element at the beginning of the list:
Example
// Importing LinkedList class
import java.util.LinkedList;
public class tutorialproTest {
public static void main(String[] args) {
LinkedList<String> sites = new LinkedList<String>();
sites.add("Google");
sites.add("tutorialpro");
sites.add("Taobao");
sites.add("Weibo");
// Using getFirst() to get the head element
System.out.println(sites.getFirst());
}
}
The output of the above example is:
Google
Getting the element at the end of the list:
Example
// Importing LinkedList class
import java.util.LinkedList;
public class tutorialproTest {
public static void main(String[] args) {
LinkedList<String> sites = new LinkedList<String>();
sites.add("Google");
sites.add("tutorialpro");
sites.add("Taobao");
sites.add("Weibo");
// Using getLast() to get the tail element
System.out.println(sites.getLast());
}
}
The output of the above example is:
Weibo
Iterating Elements
We can use a for
loop in conjunction with the size()
method to iterate through the elements in the list:
Example
// Import LinkedList class
import java.util.LinkedList;
public class tutorialproTest {
public static void main(String[] args) {
LinkedList<String> sites = new LinkedList<String>();
sites.add("Google");
sites.add("tutorialpro");
sites.add("Taobao");
sites.add("Weibo");
for (int size = sites.size(), i = 0; i < size; i++) {
System.out.println(sites.get(i));
}
}
}
The size()
method is used to calculate the size of the linked list.
The output of the above example is:
Google
tutorialpro
Taobao
Weibo
Alternatively, you can use a for-each loop to iterate through the elements:
Example
// Import LinkedList class
import java.util.LinkedList;
public class tutorialproTest {
public static void main(String[] args) {
LinkedList<String> sites = new LinkedList<String>();
sites.add("Google");
sites.add("tutorialpro");
sites.add("Taobao");
sites.add("Weibo");
for (String i : sites) {
System.out.println(i);
}
}
}
The output of the above example is:
Google
tutorialpro
Taobao
Weibo
Common Methods
Method | Description |
---|---|
public boolean add(E e) | Adds an element to the end of the list, returns true if successful, false otherwise. |
public void add(int index, E element) | Inserts an element at the specified position. |
public boolean addAll(Collection extends E> c) | Adds all elements of a collection to the end of the list, returns true if successful, false otherwise. |
public boolean addAll(int index, Collection extends E> c) | Adds all elements of a collection at the specified position, returns true if successful, false otherwise. |
public void addFirst(E e) | Adds an element to the head. |
public void addLast(E e) | Adds an element to the tail. |
public boolean offer(E e) | Adds an element to the end of the list, returns true if successful, false otherwise. |
public boolean offerFirst(E e) | Inserts an element at the head, returns true if successful, false otherwise. |
public boolean offerLast(E e) | Inserts an element at the tail, returns true if successful, false otherwise. |
public void clear() | Clears the list. |
public E removeFirst() | Removes and returns the first element. |
public E removeLast() | Removes and returns the last element. |
public boolean remove(Object o) | Removes a specified element, returns true if successful, false otherwise. |
public E remove(int index) | Removes the element at the specified position. |
public E poll() | Removes and returns the first element. |
public E remove() | Removes and returns the first element. |
public boolean contains(Object o) | Determines whether the list contains a specific element. |
public E get(int index) | Returns the element at the specified position. |
public E getFirst() | Returns the first element. |
public E getLast() | Returns the last element. |
public int indexOf(Object o) | Finds the index of the first occurrence of the specified element from the beginning. |
public int lastIndexOf(Object o) | Finds the index of the last occurrence of the specified element. |
public E peek() | Returns the first element. |
public E element() | Returns the first element. |
public E peekFirst() | Returns the head element. |
public E peekLast() | Returns the tail element. |
public E set(int index, E element) | Sets the element at the specified position. |
public Object clone() | Clones the list. |
public Iterator descendingIterator() | Returns a descending iterator. |
public int size() | Returns the number of elements in the list. |
public ListIterator listIterator(int index) | Returns an iterator from the specified position to the end. |
public Object[] toArray() | Returns an array containing all of the elements in the list. |
public T[] toArray(T[] a) | Returns an array containing all of the elements in the list, converted to the specified type. |
For more API methods, see: https://www.tutorialpro.org/manual/jdk11api/java.base/java/util/LinkedList.html