Java record-49-javaslist source code profiling
Public class Sequence List <E> extends AbstractSequentialList <E> implements List <E>, Deque <E>, Cloneable, and SerializableList interfaces. Implement all optional list operations and allow all elements (including null ). In addition to the List interface, the revoke List class also provides a unified naming method for get, remove, and insert elements at the beginning and end of the List. These operations allow the link list to be used as a stack, queue, or dual-end queue. This type of Deque interface provides first-in-first-out queue operations for add and poll, as well as other stack and double-end queue operations. All operations are performed according to the dual-link list. The indexing operation in the list traverses the list from the beginning or end (from the end near the specified index ). The linked list is a linked list, which uses a two-way linked list at the underlying layer. Two constructor implementations of the shortlist: Entry as a node. The constructor creates an empty two-way linked list. The constructor with parameters first calls the constructor without parameters, and then adds the elements in the passed set to the linked list.
private transient Entry<E> header = new Entry<E>(null, null, null); private transient int size = 0; /** * Constructs an empty list. */ public LinkedList() { header.next = header.previous = header; } /** * Constructs a list containing the elements of the specified * collection, in the order they are returned by the collection's * iterator. * * @param c the collection whose elements are to be placed into this list * @throws NullPointerException if the specified collection is null */ public LinkedList(Collection<? extends E> c) { this(); addAll(c); }
The Entry is used as a linked list node. Its implementation is as follows: Entry is an internal class implementation of the sort list. The Entry implements the Node function. The element stores data information. next points to the next Node, and previous refers to the previous Node. The constructor implements Data assignment and node pointing.
private static class Entry<E> { E element; Entry<E> next; Entry<E> previous; Entry(E element, Entry<E> next, Entry<E> previous) { this.element = element; this.next = next; this.previous = previous; } }
The addBefore method is called and the header is passed in. Create an Entry node in addBefore and modify the pointer. All are two-way linked list operations. The addBefore method is also called to add a specified location. Instead of directly passing the header node, the node to add the location is passed.
/** * Appends the specified element to the end of this list. * * <p>This method is equivalent to {@link #addLast}. * * @param e element to be appended to this list * @return <tt>true</tt> (as specified by {@link Collection#add}) */ public boolean add(E e) { addBefore(e, header); return true; } /** * Inserts the specified element at the specified position in this list. * Shifts the element currently at that position (if any) and any * subsequent elements to the right (adds one to their indices). * * @param index index at which the specified element is to be inserted * @param element element to be inserted * @throws IndexOutOfBoundsException {@inheritDoc} */ public void add(int index, E element) { addBefore(element, (index==size ? header : entry(index))); } private Entry<E> addBefore(E e, Entry<E> entry) { Entry<E> newEntry = new Entry<E>(e, entry, entry.previous); newEntry.previous.next = newEntry; newEntry.next.previous = newEntry; size++; modCount++; return newEntry; }
Retrieve data elements: the sorted list is suitable for Traversing one element at a time. Even if a location is specified for query, the sorted list needs to traverse one by one, because it only knows the header position of the linked list and needs to search for other nodes through the header, for example, the getFirst and getLast methods below are the simplest and can be found directly by using the header. However, the get method at the specified position needs to be implemented by calling the entry method to locate the node. When adding the element above to the specified position, you also need to call the entry method to locate the node at the specified position. The entry method also uses headers to traverse nodes one by one.
public E get(int index) { return entry(index).element; } public E getLast() { if (size==0) throw new NoSuchElementException(); return header.previous.element; } public E getFirst() { if (size==0) throw new NoSuchElementException(); return header.next.element; } /** * Returns the indexed entry. */ private Entry<E> entry(int index) { if (index < 0 || index >= size) throw new IndexOutOfBoundsException("Index: "+index+ ", Size: "+size); Entry<E> e = header; if (index < (size >> 1)) { for (int i = 0; i <= index; i++) e = e.next; } else { for (int i = size; i > index; i--) e = e.previous; } return e; }