List of Java collections (ArrayList, LinkedList, Vector, Stack) Understanding (new)

Source: Internet
Author: User
Tags serialization

First, ArrayList bottom realization principle

Contrast

Unlike vectors,operations in ArrayList are not thread-safe ! Therefore, it is recommended that you use ArrayList in a single thread, and you can select vectors or copyonwritearraylist in multiple threads.

Summary :
ArrayList actually saves the data through an array . When we construct ArrayList, if we use the default constructor, the default capacity size of ArrayList is ten.
(02) When the ArrayList capacity is insufficient to accommodate all elements, ArrayList will reset the capacity: New capacity = "(Raw capacity x3)/2 + 1".
ArrayList's clone function, which is to clone all the elements into an array.
(ArrayList) the way to implement Java.io.Serializable. When writing to the output stream, write "capacity", then write "every element", read "capacity" and read "every element" in turn when the input stream is read.

LinkedList Introduction

LinkedList is a doubly linked list that inherits from Abstractsequentiallist. It can also be manipulated as a stack, queue, or double-ended queue.
The LinkedList implements the List interface and can queue operations on it.
LinkedList implements the Deque interface, which means that linkedlist can be used as a double-ended queue.
LinkedList implements the Cloneable interface, which covers the function clone () and can be cloned.
The LinkedList implements the Java.io.Serializable interface, which means that the LinkedList supports serialization and can be transmitted by serialization.
The LinkedList is non-synchronous.

The essence of LinkedList is a doubly linked list.
LinkedList inherits from Abstractsequentiallist and implements the Dequeue interface.
(LinkedList) contains two important members: header and size.
The header is the table header of the doubly linked list, which is an instance of the class entry corresponding to the doubly linked list node. Entry contains member variables: Previous, Next, element. Where previous is the node's previous node, next is the node's next node, and element is the value that the node contains.
Size is the number of nodes in a doubly linked list.

Before reading the source code, we first linkedlist the overall implementation of the general description:
    LinkedList is actually achieved through a doubly linked list. Since it is a doubly linked list, its sequential access is very efficient and less efficient for random access .
    since LinkedList is through a doubly linked list, it also implements the list interface { that is, it implements the get (int location), Remove (int location) and so on " index value to get, delete the node's function" }. How does the LinkedList implement these interfaces of the list, and how to associate the doubly linked list with the index value ?
    The actual principle is very simple, it is achieved through a count index value . For example, when we call get (int location), we first compare "location" and "two-way list length of 1/2", if the former is large, then from the end of the list of the link to find the position, and then start from the bottom of the list before the search, Until the location.
    This is how the "two-line linked list and index values relate".

Summary :
LinkedList is actually implemented through a doubly linked list.
It contains a very important inner class:Entry. Entry is a data structure corresponding to a doubly linked list node , which includes the following attributes: The value that the current node contains , the previous node , and the next node .
(02) from the realization of linkedlist, it can be found that there is no problem of linkedlist capacity shortage.
LinkedList's clone function, which is to clone all the elements into a new LinkedList object.
LinkedList implements Java.io.Serializable. When writing to the output stream, write "capacity", then write "the value protected by each node", read "capacity" and read "every element" in turn when the input stream is read out.
(05) Since LinkedList implements the Deque, the Deque interface defines a method for accessing elements at both ends of a double-ended queue. Provides methods for inserting, removing, and inspecting elements. Each method has two forms: one that throws an exception when the operation fails, and another that returns a special value (null or FALSE, depending on the action).

Summarize the following table:

    first Element (header) The                 last Element (tail)        throws Exception special value            throws Exception        special value insert    addfirst (e)    Offerfirst (e)    addlast (e)        offerlast (e) removal of    Removefirst ()  Pollfirst ()      Removelast ()    polllast () check    getfirst ()     peekfirst ()      getlast ()        peeklast ()

About Vector

Unlike ArrayList,operations in vectors are thread-safe

Summary :
Vector is actually the data stored by an array . When we construct VECOTR, if we use the default constructor, the vector's default capacity size is ten.
(02) Vector capacity increases when vector capacity is insufficient to accommodate all elements. if the capacity increase factor is >0, the value of the capacity is increased by the "capacity increase factor"; otherwise, the capacity size is increased by one-fold.
The vector's clone function, which is to clone all the elements into an array.

Stack detailed description

After learning the vector, we started to learn about stack. The stack is simple and it inherits from the vector.

Stack is a stack. Its characteristics are: advanced post-Exit (FILO, first in the last out).

The stack in the Java Toolkit is inherited from vectors (vector queues), since vectors are implemented by arrays, which means thatstacks are also implemented through arrays , not linked lists . Of course, we can also use LinkedList as a stack.

Summary :

The stack is actually implemented by an array.
When the push is executed (that is, the element is pushed into the stack ), it is by appending the element to the end of the array.
The element at the end of the array is returned when Peek is executed (that is, the top element of the stack is removed and no deletion is performed ).
When the pop is executed (that is, the top element of the stack is removed, and the element is deleted from the stack ), the element at the end of the array is taken out, and then the element is removed from the array.
Stack inherits from Vector, which means that the properties and functions owned by the vector are owned by the stack.

List summary (LinkedList, ArrayList, etc. usage scenarios and performance analysis)

1th Part List Summary

Let's look at the frame diagram of list

The List is an interface that inherits from the collection interface. It represents an orderly queue.
(abstractlist) is an abstract class that inherits from Abstractcollection. Abstractlist implements a function other than size (), get (int location) in the list interface.
Abstractsequentiallist is an abstract class that inherits from Abstractlist. Abstractsequentiallist implements all functions of the linked list in a linked list, based on the index value.

ArrayList, LinkedList, Vector, stack are the 4 implementation classes of list.
ArrayList is an array queue, which is equivalent to a dynamic array . It is implemented by the array, the random access efficiency is high, the random insertion, the random deletion efficiency is low.
LinkedList is a doubly linked list . It can also be manipulated as a stack, queue, or double-ended queue. LinkedList random access efficiency is low, but random insertion and random deletion is inefficient.
Vectors are vector queues, like ArrayList, which is also a dynamic array, implemented by arrays . But ArrayList is non-thread-safe, and vectors are thread-safe.
Stack is the stack, which inherits from the vector. Its characteristics are: Advanced post-Exit (FILO, first in the last out).

part 2nd list usage Scenarios

The ultimate purpose of learning something is to be able to understand and use it . The following is a summary of the use of the list of the various scenarios , and then analyze the reasons behind .

If the "stack", "queue", "linked list" and other operations, should consider using list, the specific choice of which list, according to the criteria below to choose.
(01) For A quick insert, delete the element , you should use LinkedList.
(02) for fast random access to elements, ArrayList should be used.
(03) for a "single-threaded environment " or " multithreaded environment, but the list will only be operated by a single thread ", a non-synchronized class (such as ArrayList) should be used.
For a multithreaded environment , and the list may be manipulated by more than one thread at a time, you should use a synchronized class (such as a vector).

Why are linkedlisk insertions and deletions faster? : You do not need to move the contents of the following when the list is inserted.

The ArrayList array, in this case, needs to move all the array contents after insertion, which takes more time .

Why ArrayList query faster? Because the ArrayList array returns the first element directly, and the list finds the location, The value is taken (the lookup is time consuming)

List of Java collections (ArrayList, LinkedList, Vector, Stack) Understanding (new)

Related Article

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.