底层实现
LinkedList的继承关系如下图所示:
源码:
public class LinkedList<E> extends AbstractSequentialList<E> implements List<E>, Deque<E>, Cloneable, java.io.Serializable{
transient int size = 0;
/**
* Pointer to first node.
* Invariant: (first == null && last == null) ||
* (first.prev == null && first.item != null)
*/
transient Node<E> first;
/**
* Pointer to last node.
* Invariant: (first == null && last == null) ||
* (last.next == null && last.item != null)
*/
transient Node<E> last;
/**
* Constructs an empty list.
*/
public LinkedList() {
}
/**
* Constructs a list containing the elements of the specified
* collection, in the order they are returned by the collection's
* iterator.
*
* @param c the collection whose elements are to be placed into this list
* @throws NullPointerException if the specified collection is null
*/
public LinkedList(Collection<? extends E> c) {
this();
addAll(c);
}
}
LinkedList也是List接口的实现类,而LinkedList数据结构是双向链表。
特点:
- 实现了List,因此可以进行队列操作
- 实现Deque接口,能将LinkedList当做双端队列使用
- 继承于AbstractSequentialList的双向链表
- 覆盖了函数clone(),能被克隆
- 操作不是线程安全的
LinkedList继承了AbstractSequentialList类,实现了List接口,AbstractSequentialList中已经实现了很多方法,如get(int index)、set(int index, E element)、add(int index, E element) 和 remove(int index),这些方法是我们集合操作时使用最多的,不过这些方法在LinkedList中都已经被重写了,而抽象方法在LinkedList中有了具体实现。
使用指南
属性
size:集合的长度
first:双向链表头部节点
last:双向链表尾部节点
方法
给链表中添加元素
●boolean add(E e)
在链表尾部添加一个元素,如果成功,返回true,否则返回false。
●void add(int index, E element)
在指定位置插入一个元素。
●void addFirst(E e)
在链表头部插入一个元素。
●addLast(E e)
在链表尾部添加一个元素。
删除链表中的元素
●boolean remove(Object o)
从当前链表中移除指定的元素。
● E remove(int index)
从当前链表中移除指定位置的元素。
● E remove()
从当前链表中移除第一个元素,同removeLast()相同。
● E removeFirst()
从当前链表中移除第一个元素。
● E removeLast()
从当前链表中移除最后一个元素。
获取链表中元素
● E get(int index)
从当前链表中获取指定位置的元素。
● E getFirst()
从当前链表中获取第一个元素。
● E getLast()
从当前链表中获取最后一个元素。
关于队列的操作
队列:先进先出,后进后出。它只允许在表的前端进行删除操作,而在表的后端进行插入操作。
LinkedList类实现了Queue接口,因此我们可以把LinkedList当成Queue(队列)来用。
● E peek()
获取队列的第一个元素,如果为null会返回null
● E element()
获取队列的第一个元素,如果为null会抛出异常
● E poll()
获取队列的第一个元素,并在队列中删除,如果为null会返回null
● E remove()
获取队列的第一个元素,并在队列中删除,如果为null会抛出异常
● boolean offer(E e)
将元素添加到队列尾部,并返回是否添加成功
实例代码:
import java.util.LinkedList;
import java.util.Queue;
public class Main {
public static void main(String[] args) {
//add()和remove()方法在失败的时候会抛出异常(不推荐)
Queue<String> queue = new LinkedList<String>();
//添加元素
queue.offer("a");
queue.offer("b");
queue.offer("c");
queue.offer("d");
queue.offer("e");
for(String q : queue){
System.out.println(q);
}
System.out.println("===");
System.out.println("poll="+queue.poll()); //返回第一个元素,并在队列中删除
for(String q : queue){
System.out.println(q);
}
System.out.println("===");
System.out.println("element="+queue.element()); //返回第一个元素
for(String q : queue){
System.out.println(q);
}
System.out.println("===");
System.out.println("peek="+queue.peek()); //返回第一个元素
for(String q : queue){
System.out.println(q);
}
}
}
运行输出结果为:
a
b
c
d
e
===
poll=a
b
c
d
e
===
element=b
b
c
d
e
===
peek=b
b
c
d
e
offer,add 区别:
一些队列有大小限制,因此如果想在一个满的队列中加入一个新项,多出的项就会被拒绝。
这时新的 offer 方法就可以起作用了。它不是对调用 add() 方法抛出一个 unchecked 异常,而只是得到由 offer() 返回的 false。
poll,remove 区别:
remove() 和 poll() 方法都是从队列中删除第一个元素。remove() 的行为与 Collection 接口的版本相似, 但是新的 poll() 方法在用空集合调用时不是抛出异常,只是返回 null。因此新的方法更适合容易出现异常条件的情况。
peek,element区别:
element() 和 peek() 用于在队列的头部查询元素。与 remove() 方法类似,在队列为空时, element() 抛出一个异常,而 peek() 返回 null。
关于栈的操作
栈:后进先出。所以作为栈使用也很简单,添加和删除元素都只操作队列的首节点即可。
● addFirst() //添加元素到列表的起始位置
● addLast() //添加元素到列表的结束位置
● removeFirst() //移除列表起始位置的元素
● removeLast() //移除列表结束位置的元素
● getFirst() //获取列表起始位置的元素
● getLast() //获取列表结束位置的元素