Java Linkedlist
[ Java Collections Framework](#)
Linked list is a common fundamental data structure, it is a linear list, but it does not store data in linear order. Instead, each node stores the address of the next node.
Linked lists can be divided into singly linked lists and doubly linked lists.
A singly linked list contains two values: the value of the current node and a link to the next node.
!(#)
A doubly linked list has three integer values: the value, a link to the next node, and a link to the previous node.
!(#)
Java LinkedList is similar to ArrayList and is a commonly used data container.
Compared with ArrayList, LinkedList has higher efficiency for add and delete operations, but lower efficiency for search and modify operations.
**Use ArrayList in the following cases:**
* Frequently access an element in the list.
* Only need to add and delete elements at the end of the list.
**Use LinkedList in the following cases:**
* You need to iterate through the list to access certain elements.
* You need to frequently add and delete elements at the beginning, middle, or end of the list.
LinkedList inherits 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 queue.
LinkedList implements the Cloneable interface and can implement cloning.
LinkedList implements the java.io.Serializable interface, which means it supports serialization and can be transmitted through serialization.
!(#)
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 list = new LinkedList(); // Regular creation method
or
LinkedList list = new LinkedList(Collection c); // Create list using collection
Create a simple LinkedList instance:
## Example
// Import LinkedList class
import java.util.LinkedList;
public class TutorialTest {
public static void main(String[] args){
LinkedList sites =new LinkedList();
sites.add("Google");
sites.add("Tutorial");
sites.add("Taobao");
sites.add("Weibo");
System.out.println(sites);
}
}
For the above example, the output is:
[Google, Tutorial, Taobao, Weibo]
In most cases, ArrayList is more efficient for accessing random elements in the list, but LinkedList provides more efficient methods in the following situations.
Add element at the beginning of the list:
## Example
// Import LinkedList class
import java.util.LinkedList;
public class TutorialTest {
public static void main(String[] args){
LinkedList sites =new LinkedList();
sites.add("Google");
sites.add("Tutorial");
sites.add("Taobao");
// Use addFirst() to add element at the beginning
sites.addFirst("Wiki");
System.out.println(sites);
}
}
For the above example, the output is:
[Wiki, Google, Tutorial, Taobao]
Add element at the end of the list:
## Example
// Import LinkedList class
import java.util.LinkedList;
public class TutorialTest {
public static void main(String[] args){
LinkedList sites
YouTip