Java List Collection Detailed

Source: Internet
Author: User

ArrayList: Implement randomaccess to quickly find elements

Initialize: Default initialization size is 10

Data structure: Using the linear structure of the array to store the information

Ordered: array structure is orderly
Expansion mode: Expands int newcapacity = oldcapacity + (oldcapacity >> 1) When adding an element over the current capacity; Enlarged for the original 3/2

Thread Safety: Non-thread-safe can be passed collections.synchronizedlist (a); Method for thread-safe operation
Advantage: Array structure lookup is fast
Cons: Slower to delete and increase (except when added at the end)
When you delete an element of a location in ArrayList and add an element of a seat, execute:
1. Check if subscript is out of bounds
2. Check if expansion is required
3. Create a new array to copy the original array elements.
4. Perform add and delete operations

LinkedList: Implements the Deque interface to operate on both ends of the element
Initialize: Defines the first node element and the last node element
Data structure: Doubly linked list structure
Why is a doubly linked list structure

private static class Node<e> {E item; Node<e> Next;  The previous node node<e> prev; Node (node<e> prev, E element, node<e> next) {This.item = Element;this.next = Next;this.prev = prev;}}

  

Ordered: The list structure will have a pointer to the previous element and the latter element is ordered
Capacity expansion: Add a node directly, and point the new node's forward pointer to the last element
Thread Safety: Non-thread safe
Pros: When adding and removing elements from the list structure, you only need to change the point of the previous element in node and the latter

--New public boolean add (E e) {linklast (e); return true;} void Linklast (e e) {final node<e> L = last;//create a forward pointer to the final element of the current collection, and a null back pointer to the node final node<e> newNode = new No De<> (l, E, null); Now the last element in the collection is the new nodelast = newnode;if (L = = null) First = newnode;else//if null at the current last node points to the new nodel.next for the current last node's backward pointer = newnode;size++;modcount++;} --Delete E unlink (node<e> x) {//assert x! = null;final E element = x.item;final node<e> next = x.next;final NODE&L T E> prev = x.prev;if (prev = = null) {first = next;} else {prev.next = next;//) point the previous element to the next element of this element X.prev = null;//will forward this element The pointer is null}if (next = null) {last = prev;} else {Next.prev = prev;//the next element of this element points to the previous element of the element x.next = null;//The backward pointer of this element is empty}x.item = Null;size--;modcount++;return element;} Cons: Query slower public E get (int index) {checkelementindex (index); return node (index). Item;} The find element loops over half of all elements to find node<e> node (int index) {//Assert Iselementindex (index), if (Index < (size >> 1)) {Nod e<e> x = first;for (int i = 0; I &lT Index i++) x = X.next;return x;} else {node<e> x = last;for (int i = size-1; i > Index; i--) x = X.prev;return x;}}

  

Java List Collection Detailed

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.