LinkedList全面说明
- LinkedList底层实现了双向链表和双端队列特点
- 可以添加任意元素(元素可以重复),包括null
- 线程不安全,没有实现同步
LinkedList的底层操作机制
- LinkedList底层维护了一个双向链表.
- LinkedList中维护了两个属性first和last分别指向首节点和尾节点
- 每个节点(Node对象),里面又维护了prev、next、item三个属性,其中通过prev指向前一个,通过next指向后一个节点。最终实现双向链表.
- 所以LinkedList的元素的添加和删除,不是通过数组完成的,相对来说效率较高。
模拟一个简单的双向链表
package com.Collection_.List_;
/**
* @author pengyang
* @date 2022-06-09 09:27
*/
public class LinkedList01 {
public static void main(String[] args) {
//模拟一个简单的双向链表
Node jack = new Node("jack");
Node tom = new Node("tom");
Node hsp = new Node("hsp");
//连接三个节点,形成双向链表
jack.next = tom;
tom.next = hsp;
hsp.pre = tom;
tom.pre = jack;
Node first = jack;//双向链表的头节点
Node last = hsp;//双向链表的尾节点
//从头到尾遍历
System.out.println("---------从头到尾遍历--------");
while (first != null) {
System.out.println(first);
first = first.next;
}
//从尾到头遍历
System.out.println("---------从头到尾遍历--------");
while (last != null) {
System.out.println(last);
last = last.pre;
}
//链表添加数据/对象
Node smith = new Node("smith");
smith.next = hsp;
smith.pre = tom;
tom.next = smith;
hsp.pre = tom;
first = jack;//双向链表的头节点
//从头到尾遍历
System.out.println("---------从头到尾遍历--------");
while (first != null) {
System.out.println(first);
first = first.next;
}
}
}
class Node {
public Object item;
public Node next;
public Node pre;
public Node(Object item) {
this.item = item;
}
@Override
public String toString() {
return "Node{" +
"item=" + item +
'}';
}
}
LinkedList 源码
package com.Collection_.List_;
import java.util.LinkedList;
/**
* @author pengyang
* @date 2022-06-10 13:12
*/
public class LinkedListCRUD {
public static void main(String[] args) {
LinkedList linkedList = new LinkedList();
linkedList.add(1);
linkedList.add(2);
linkedList.add(3);
linkedList.remove(); //这里默认删除第一个节点
System.out.println("linkedList" + linkedList);
/*
//添加
1.LinkedList linkedList = new LinkedList();
此时linkeList 的属性 first = null last = null
2.执行添加
public boolean add(E e) {
linkLast(e);
return true;
}
3.将新的节点,加入到双向链表最后
void linkLast(E e) {
final Node<E> l = last;
final Node<E> newNode = new Node<>(l, e, null);
last = newNode;
if (l == null)
first = newNode;
else
l.next = newNode;
size++;
modCount++;
}
//删除
linkedList.remove();
1.执行
public E remove() {
return removeFirst();
}
2.执行
public E removeFirst() {
final Node<E> f = first;
if (f == null)
throw new NoSuchElementException();
return unlinkFirst(f);
}
3.执行
private E unlinkFirst(Node<E> f) {
// assert f == first && f != null;
final E element = f.item;
final Node<E> next = f.next;
f.item = null;
f.next = null; // help GC
first = next;
if (next == null)
last = null;
else
next.prev = null;
size--;
modCount++;
return element;
}
*/
}
}
ArrayList和LinkedList的比较
如何选择ArrayList和LinkedList:
- 如果我们改查的操作多,选择ArrayList
- 如果我们增删的操作多,选择LinkedList
- 一般来说,在程序中,80%-90%都是查询,因此大部分情况下会选择ArrayList
- 在一个项目中,根据业务灵活选择,也可能这样,一个模块使用的是ArrayList,另外一个模块是LinkedList,也就是说,要根据业务来进行选择