LinkedList source code analysis of Java collection
summary
LinkedList, like ArrayList, implements the list interface, but its internal data structure is different. LinkedList is implemented based on linked list (as can be seen from the name), and the random access efficiency is worse than ArrayList Its insert and delete operations are more efficient than ArrayList, but it still needs to traverse the pointer of part of the linked list to move to the position indicated by the subscript. Only the operations at both ends of the linked list can eliminate the movement, such as add(), addfirest(), removelast(), etc
LinkedList source code analysis
1. Data structure
LinkedList is based on the linked list structure and defines the head and tail pointers in the class It internally maintains a two-way linked list
2. Construction method
The default constructor is simple and has nothing
Add the elements of the collection to your LinkedList:
3. Storage
(1) Add (E) adds an element to the end of the linked list
(2) Add (int, e) inserts the element at the specified location
(3) Addall (Collection) adds the collection to the end of the linked list. This method is introduced in the construction method and will not be repeated here
(4) Addall (int, collection) adds the collection to the specified location of the linked list. This method is also introduced in the construction method
(5) Addfirst (E) adds an element to the header of the linked list
(6) Addlast (E) adds an element to the footer
4. Access
5. Delete
The deletion method is not given in the source code, which is basically the same There are the following methods:
LinkedList is a powerful class, which can be used as list collection, queue and stack