About LinkedList for OpenJDK

Outline

     LinkedList bottom using two-way linked list structure, the array structure and not the same ArrayList. LinkedList due to different data structures, the application does not require contiguous memory, memory chips may be utilized. Element holds data required to the contents stored on a reference downstream data link is formed. LinkedList not thread-safe.
 
Class Diagram
 

 

 

size variable
    Like ArrayList of variable size, size the number of elements in this sense.
 
first variable
     The first storage element reference
 
last variable
     The last element stored reference
 
Node node class
     LinkedList achieve an internal class Node, and the item stored in the downstream storage instance object references. variable references cited under item storage element instance, next variable node, prev a referenced node. See below Source:
 

 

 

Pertaining to private internal use only LinkedList Node
static modification, because the static keyword do not know what purpose.
 
Adding elements
add function is very simple, function calls linkLast, did not return a boolean true exception. 
 

 

 

linkLast function to achieve additional element appended to the last element of the chain, and establish a link relationship. size variable accumulation.
The first scenario: e is an element of the first, last variable is null, newNode assigning variable references first, last variable reference newNode.
The second scenario: LinkedList stored in the first reference, i.e., elements that are not the first element e. next reference to the instance variable l newNode, newNode last assigned to the last element. newNode the next variable is null.
 

 

 

 
Batch add elements
1) checkPositionIndex check index is not greater than size-1
 
2) Node Get index variable transmission instance corresponding to the position of the function node. See source below:
index<(size>>1)当成if条件,这里使用一次二分查找方式来快速查找Node实现,LinkedList元素链长的时间特别有用。接下来是一个从头开始查找,另一个从尾开始查找。
 

 

 

3)逐个追加元素
 
4)addAll函数源码
 

 

 

 
指定索引位置代替元素
使用set函数实现插入元素。使用checkElementIndex检查index,然后使用node函数获取对应index位置Node实例,最后element赋值给item变量。
 

 

 

删除指定索引元素的remove函数
1)使用checkElementIndex检查index,node函数获取index对应位置的Node实例,
2)unlink函数删除链路关系。
 
指定index索引获取元素
1)checkElementIndex检查索引
2)node函数通过index获取Node实例,返回node.item元素。
 

 

 

 
总结:
     1)插入,代替,删除操作耗时少
     2)查找操作耗时比较多
 
 
 
 
 
 
 

Guess you like

Origin www.cnblogs.com/wspgbw/p/12083002.html