In-depth understanding of LinkedList in Java collections

一、LinkedList

1. Storage principle

LinkedList is a linked list, with a front node and a rear node before and after the elements, which are used to connect the previous element and the next element in the set, and "hand in hand" in turn to form a set of chained data.

2. Unique method

Insert picture description here

3. Comparison with ArrayList collection

1. The List interface stores a set of objects that are not unique (can be repeated) and ordered (insertion order)

2. ArrayList implements a variable-length Object type array, allocates continuous space in memory, and traverses elements and randomly accesses elements more efficiently

3. LinkedList adopts linked list storage method, which is more efficient when inserting and deleting elements

4. Compare the difference between the two structures by viewing the source code of the adding method. Chain storage and array storage

Insert picture description here
Reasons for different efficiency:

Linkedlist will only affect the two adjacent elements when performing delete and add operations.

Since Arraylist is an array in nature, the address values ​​of all subsequent elements will be shifted when performing delete and add operations.

4. In-depth understanding through source code and drawing

Insert picture description here

Insert picture description here
Insert picture description here

code show as below(Insert picture description here

to sum up

The above is all the content of the in-depth understanding of LinkedList in the Java collection, mainly using its principles, methods, and source code and drawing methods for in-depth understanding.

Guess you like

Origin blog.csdn.net/StruggleBamboo/article/details/111566396