LinkedList是基于链表实现的。链表是一种线性的存储结构,它的一个存储单元里面,除了存放有待存储的数据以外,还存储有其下一个存储单元的地址(下一个存储单元的地址是必要的,有些存储结构还存放有其前一个存储单元的地址),每次查找数据的时候,通过某个存储单元中的下一个存储单元的地址寻找其后面的那个存储单元。
LinkedList是一种有序的,可重复的双向链表,双向是指:
- 链表中任意一个存储单元都可以通过向前或者向后寻址的方式获取到其前一个存储单元和其后一个存储单元;
- 链表的尾节点的后一个节点是链表的头结点,链表的头结点的前一个节点是链表的尾节点。
构造器
-
LinkedList()
创建一个空的链表; -
LinkedList(Collection<? extends E> collection)
调用第一个构造方法构造一个空的链表,之后通过addAll将c中的元素全部添加到链表中。
常用方法
除了在ArrayList
和HashSet
中了解到的collection
的通用方法,add
, addAll
, clear
, contains
, size
, isEmpty
外,LinkedList
还有一些更细化的方法:
-
add(E e)
:在链表后添加一个元素;-
add(int index, E element)
:在指定位置插入一个元素; -
addFirst(E e)
:在链表头部插入一个元素; -
addLast(E e)
:在链表尾部添加一个元素;
-
-
push(E e)
:与addFirs
t方法一致 -
offer(E e)
:在链表尾部插入一个元素 -
offerFirst(E e)
:JDK1.6版本之后,在头部添加; -
offerLast(E e)
:JDK1.6版本之后,在尾部添加; -
remove()
:移除链表中第一个元素;-
remove(E e)
:移除指定元素; -
removeFirst(E e)
:删除头,获取元素并删除; -
removeLast(E e)
:删除尾;
-
-
pop()
:和removeFirst
方法一致,删除头; -
poll()
:查询并移除第一个元素-
pollFirst()
:删除头; -
pollLast()
:删除尾;
-
-
get(int index)
:按照下标获取元素;-
getFirst()
:获取第一个元素; -
getLast()
:获取最后一个元素;
-
-
peek()
:获取第一个元素,但是不移除; -
peekFirst()
:获取第一个元素,但是不移除; -
peekLast()
:获取最后一个元素,但是不移除;
其中,使用get
和remove
方法,若列表为空,则抛出空元素异常
,而peek
和poll
则返回null
。
例子:
import java.util.LinkedList;
public class Test {
public static void main(String[] args) {
// 创建一个LinkedList
LinkedList colors = new LinkedList();
System.out.println("Initial size of colors: " + colors.size());
// 向LinkedList添加元素
colors.add("blue");
colors.add("yellow");
colors.add("green");
colors.add("red");
System.out.println("colors: " + colors);
System.out.println("colors contains pink: " + colors.contains("pink"));
colors.addFirst("pink");
// 查询LinkedList第一个元素
System.out.println("First element of colors: " + colors.getFirst()); // 查询第一个元素但不删除
System.out.println("First element of colors: " + colors.poll()); // 查询并删除第一个元素
System.out.println("First element of colors: " + colors.getFirst());
System.out.println("colors: " + colors);
}
}
结果:
Initial size of colors: 0
colors: [blue, yellow, green, red]
colors contains pink: false
First element of colors: pink
First element of colors: pink
First element of colors: blue
colors: [blue, yellow, green, red]
遍历
LinkedList
与ArrayList
相同,也可使用以下三种方式遍历:
- for循环:
for (int i = 0; i < collection.size(); i++) {
// some code
// example: collection.get(i);
}
- 自动增强的for循环
for (Object obj : collection) {
// some code
}
- 迭代器
Iterator<Object> it = collection.iterator();
while(it.hasNext()){
// some code
// example it.next()
}
另外,LinkedList
还可以通过removeFisrt
, removeLast
, pollFisrt
, pollLast
, 优点是速度快效率高,尤其是remove
,缺点是会删除原始数据。如果只是想遍历查询而不删除数据,推荐使用自二中增强的for循环遍历(效率高),不太推荐使用第一种for循环(效率很低)。
例子:
import java.util.Iterator;
import java.util.LinkedList;
public class Test {
public static void main(String[] args) {
LinkedList colors = new LinkedList();
colors.add("blue");
colors.add("yellow");
colors.add("green");
colors.add("red");
System.out.println("==> For Loop Example");
for (int i = 0; i < colors.size(); i++) {
System.out.println(colors.get(i));
}
System.out.println("==> Advance For Loop Example");
for (Object color : colors) {
System.out.println(color);
}
System.out.println("==> Iterator Example");
Iterator<String> colorsIterator = colors.iterator();
while (colorsIterator.hasNext()) {
System.out.println(colorsIterator.next());
}
System.out.println("==> removeFist Example");
while (colors.size() != 0) {
System.out.println(colors.removeFirst());
}
}
}
结果:
yellow
green
red
==> Iterator Example
blue
yellow
green
red
==> removeFist Example
blue
yellow
green
red