前言
Java的LinkedList学习笔记
继承结构
graph TD
Object --> AbstractCollection
AbstractCollection --> AbstractList
AbstractList --> AbstractSequentialList
AbstractSequentialList --> LinkedList
Iterable --> Collection
Collection --> List
List --> LinkedList
Collection --> Queue
Queue --> Deque
Deque --> LinkedList
style Object fill:#f0f8ff,stroke:#696969
style AbstractCollection fill:#f0f8ff,stroke:#696969
style AbstractList fill:#f0f8ff,stroke:#696969
style AbstractSequentialList fill:#f0f8ff,stroke:#696969
style LinkedList fill:#f0f8ff,stroke:#696969
style Iterable fill:#f0f8ff,stroke:#4169e1
style Collection fill:#f0f8ff,stroke:#4169e1
style List fill:#f0f8ff,stroke:#4169e1
style Queue fill:#f0f8ff,stroke:#4169e1
style Deque fill:#f0f8ff,stroke:#4169e1
创建对象
1
| LinkedList<Object> list = new LinkedList<>();
|
基于已有集合创建
1 2 3
| Collection<Object> c;
LinkedList<Object> list = new LinkedList<>(c);
|
Collection的相关方法
传送门
List的相关方法
传送门
遍历
通过迭代器遍历
1 2 3 4 5 6 7
| Iterator iterator = list.iterator(); while (iterator.hasNext()) { Object item = iterator.next();
...
}
|
通过for…each语法遍历
1 2 3
| for (Object item : list) { ... }
|
通过forEach方法遍历
forEach()内传入的Lambda表达式内不能访问非final修饰的变量,类属性或实例属性无限制
- 如果变量没有显式通过
final修饰,但是在Lambda表达式内被访问,则Java会为变量隐式添加final修饰
1 2 3
| list.forEach((item) -> { ... });
|
List集合与数组转换
传送门
Queue的相关方法
添加元素
尾部添加元素(入队)
1
| boolean success = list.offer(<value>);
|
1
| boolean success = list.add(<value>);
|
删除元素
删除头部元素并返回(出队)
1
| Object item = list.poll();
|
1
| Object item = list.remove();
|
获取元素
获取头部元素
1
| Object item = list.peek();
|
1
| Object item = list.element();
|
Deque的相关方法
添加元素
头部添加元素
1
| boolean success = list.offerFirst(<value>);
|
尾部添加元素
1
| boolean success = list.offerLast(<value>);
|
删除元素
删除头部元素并返回
1
| Object item = list.pollFirst();
|
1
| Object item = list.removeFirst();
|
删除尾部元素并返回
1
| Object item = list.pollLast();
|
1
| Object item = list.removeLast();
|
获取元素
获取头部元素
1
| Object item = list.peekFirst();
|
1
| Object item = list.getFirst();
|
获取尾部元素
1
| Object item = list.peekLast();
|
1
| Object item = list.getLast();
|
完成