This article has participated in the "Newcomer Creation Ceremony" activity, and started the road of Nuggets creation together
Java collection - Vector, LinkedList (JDK17 source code interpretation)
Vector
Array structure implementation, fast query, slow addition and deletion, slow running efficiency, thread safety
Example
Because of the similarity between Vector and ArrayList, I put the instance directly
package list;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Vector;
public class demno2 {
public static void main(String[] args) {
Vector<String> vector = new Vector<>();
vector.add("a");
vector.add("b");
vector.add("c");
//直接看遍历
Enumeration<String> elements = vector.elements();
while (elements.hasMoreElements()){
System.out.println(elements.nextElement());
}
}
}
复制代码
LinkedList
Linked list structure implementation, fast addition and deletion, slow query
Common method
Create method
LinkedList<E> list = new LinkedList<E>(); // 普通创建方法
或者
LinkedList<E> list = new LinkedList(Collection<? extends E> c); // 使用集合创建链表
复制代码
1.addFirst method adds elements to the head
LinkedList<String> list1 = new LinkedList<>();
list1.add("a1");
list1.add("a2");
list1.add("a3");
list1.addFirst("n1");
for (String x : list1) {
System.out.println(x);
}
复制代码
2.addLast method adds elements at the end
LinkedList<String> list1 = new LinkedList<>();
list1.add("a1");
list1.add("a2");
list1.add("a3");
list1.addLast("b1");
for (String x : list1) {
System.out.println(x);
}
复制代码
3. The removeFirst method removes the elements of the head
LinkedList<String> list1 = new LinkedList<>();
list1.add("a1");
list1.add("a2");
list1.add("a3");
list1.removeFirst();
for (String x : list1) {
System.out.println(x);
}
复制代码
4. The removeLast method removes the tail element
LinkedList<String> list1 = new LinkedList<>();
list1.add("a1");
list1.add("a2");
list1.add("a3");
list1.removeLast();
for (String x : list1) {
System.out.println(x);
}
复制代码
5. getFirst method to get the head element
LinkedList<String> list1 = new LinkedList<>();
list1.add("a1");
list1.add("a2");
list1.add("a3");
System.out.println(list1.getFirst());
复制代码
6. getLast method to get the tail element
LinkedList<String> list1 = new LinkedList<>();
list1.add("a1");
list1.add("a2");
list1.add("a3");
System.out.println(list1.getLast());
复制代码
Other methods
- public boolean offer(E e)
向链表末尾添加元素,返回是否成功,成功为true,失败为 false。
- public boolean offerFirst (E e)
头部插入元素,返回是否成功,成功为true,失败为false。
- public boolean offerLast(E e)
尾部插入元素,返回是否成功,成功为true,失败为 false
- public E removeFirst()
删除并返回第一个元素
- public E removeLast()
删除并返回最后一个元素
- public E poll()
删除并返回第一个元素
- public E remove()
删除并返回第一个元素
- public E peek()
返回第一个元素
- public E element()
返回第一个元素
- public E peekFirst()
返回头部元素
- public E peekLast()
返回尾部元素
Inkedlist and Arraylist use range
Use ArrayList in the following situations:
- Frequent access to an element in a list.
- Just add and remove elements from the end of the list.
Use LinkedList in the following situations:
- You need to iterate through a loop to access certain elements in the list.
- It is necessary to frequently add and delete elements at the beginning, middle, and end of the list.
Source code interpretation
The container size defaults to 0
transient int size = 0;
复制代码
head node
transient Node<E> first;
复制代码
tail node
transient Node<E> last;
复制代码
No-argument constructor
That's right, no-argument constructs are empty
public LinkedList() {
}
复制代码
How to add elements in LinkedList (add method)
public boolean add(E e) {
linkLast(e);
return true;
}
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++;
}
private static class Node<E> {
E item;
Node<E> next;
Node<E> prev;
Node(Node<E> prev, E element, Node<E> next) {
this.item = element;
this.next = next;
this.prev = prev;
}
}
复制代码
Let's take a look
The first line of add calls the linkLast method
The second line Nfinal Node< E > l = last; get the last node
This is the actual structure
private static class Node<E> {
E item;
Node<E> next;
Node<E> prev;
Node(Node<E> prev, E element, Node<E> next) {
this.item = element;
this.next = next;
this.prev = prev;
}
}
复制代码
From this we can see that Node consists of three blocks:
- item data field
- Node< E > next Next Node
- Node< E > prev 前一个 Node
这是因为Java中没有(显)指针的概念,所以无法用地址的引用来对前一个Node和后一个Node进行寻址获取 而是以这样一种形式链到了一起成了“三人帮“ 所以当你找到一个Node的时候也就找到了他相邻的两个Node
俗称一人犯罪连累邻里,连坐由此产生
第三行final Node< E > newNode = new Node<>(l, e, null);创建一个新结点
这个新结点可以看出他的上一个结点是上面第二行的 l 结点,也就是最后一个结点,自己呢就是传入进来的e,而他没有尾节点,所以是null
第四行 last = newNode;让尾节点变成新结点
第五部分if判断
如果你是添加第一个元素,现在 l 就是空的那么
first = newNode;
复制代码
头节点就会获取新结点然后容器大小size+1,修改次数+1
如果你之前已经有元素了,这说明尾节点非空,那么 l 也是非空
l.next = newNode;
复制代码
那么 l 的下一个就是新的结点