Java Common List Collection usage scenario analysis

Source: Internet
Author: User
Tags array length prev

Java Common List Collection usage scenario analysis

The last article before the Chinese New Year, this chapter introduces the Arraylist,linkedlist,vector,copyonwritearraylist bottom realization principle and the difference of four sets. Make it clear to you why ArrayList and copyonwritearraylist are commonly used in work? Understanding the underlying implementation principle, we can learn a lot of code design ideas, broaden their thinking. This chapter is easy to understand, still waiting for what, learn it quickly!

Knowledge Plots:

Technology: Arraylist,linkedlist,vector,copyonwritearraylist
Description: This chapter is based on the simple source code with arraylist,linkedlist on Jdk1.8,github
Source: Https://github.com/ITDragonBlog/daydayup/tree/master/Java/collection-stu

Knowledge Preview

ArrayList : A non-thread-safe collection that is based on an array implementation. Query elements fast, INSERT, delete intermediate elements slowly.
LinkedList : A non-thread-safe collection that is based on a linked list. Query element slow, insert, delete middle element fast.
Vector : A collection of thread-safe implementations based on arrays. Thread synchronization (method is synchronized decorated), performance is worse than ArrayList.
copyonwritearraylist : The collection of thread-safe write-time replicas based on array implementations. Thread-Safe (reentrantlock locking), performance is higher than vector, suitable for reading and writing less scenes.

The essence of ArrayList and LinkedList reading and writing speed

ArrayList: The query data is fast because the array can be directly found by the subscript element. There are two reasons for slow write data: One is that the array copy process takes time, and the other is that it takes time for the expansion to instantiate a new array.
LinkedList: The query data is slow because the list needs to traverse each element until it is found. There is a reason for writing data fast: In addition to the time required to instantiate an object, you simply need to modify the pointer to complete adding and deleting elements.
This chapter will verify the above statement through source code analysis.

Note: The block here and the slow is relative. Not LinkedList inserts and deletes must be faster than ArrayList. Understand the nature of its speed:ArrayList fast in the positioning, slow in the array copy. LinkedList slow in position, fast in pointer modification .

ArrayList

ArrayList is a non-thread-safe collection that is based on dynamic array implementations. When the underlying array is filled with elements that continue to be added, ArrayList expands its array length by performing an expansion mechanism. ArrayList query is very fast, making it widely used in real development. The drawback is that inserting and deleting elements is slow, and it is not thread-safe.
We can find the answer from the source code.

//Query element PublicEGet(intIndex) {Rangecheck(index);//Check whether it is out of bounds    return Elementdata(index);}//Order add element Public Boolean Add(e) {ensurecapacityinternal(Size +1);//expansion mechanismelementdata[size++] = e;return true;}//Add elements from the middle of the array Public void Add(intIndex, E Element) {Rangecheckforadd(index);//array subscript out of bounds check    ensurecapacityinternal(Size +1);//expansion mechanismSystem.arraycopy(Elementdata, index, elementdata, index +1, Size-index);//Copy arrayElementdata[index] = element;//Replace elementsize++;}//Delete an element from the arrayPrivate void Fastremove(intindex) {modcount++;intnummoved = Size-index-1;if(Nummoved >0) System.arraycopy(Elementdata, Index+1, Elementdata, index, nummoved); Elementdata[--size] =NULL;//Clear to let GC do it work}

From the source can be learned that
ArrayList when performing a query operation:
The first step: first determine whether the subscript is out of bounds.
Second step: The element is then returned directly from the array by subscript.

ArrayList when performing sequential add operations:
The first step: the expansion mechanism to determine whether the original array has space, if not, re-instantiate a larger space of the new array, the old array of data copied into the new array.
Step Two: Add a value to the last element of the new array.

ArrayList when performing an intermediate insert operation:
The first step: first determine whether the subscript is out of bounds.
Step two: Capacity expansion.
Step three: If the subscript labeled I is inserted, then all elements after I are moved back one by the way of copying the array.
Fourth step: Replace the old element labeled I with the new data.
The same is true for delete: Only the array moves forward one bit, and the last element is set to NULL, waiting for JVM garbage collection.

From the above source code analysis, we can get a conclusion and a question.
The conclusion is: ArrayList fast in subscript positioning, slow copying in array.
The question is: Can the length of each expansion be set to a large point, reducing the number of expansions, thereby increasing efficiency? In fact, each expansion of the length of the size is very fastidious. If the length of the expansion is too large, it will cause a lot of idle space, if the length of the expansion is too small, it will cause frequent expansion (array replication), less efficient.

LinkedList

LinkedList is a non-thread-safe collection based on a doubly linked list, which is a linked list structure that cannot be accessed randomly like an array, and must be traversed by each element until the element is found. The particularity of its structure causes it to query data slowly.
We can find the answer from the source code.

//Query element PublicEGet(intIndex) {Checkelementindex(index);//Check whether it is out of bounds    return node(index).Item;} Node<e>node(intIndex) {if(Index < (size >>1)) {//Similar dichotomynode<e> x = First; for(inti =0; I < index; i++) x = x.Next;returnX }Else{node<e> x = last; for(inti = size-1; i > Index; i--) x = x.prev;returnX }}//Insert Element Public void Add(intIndex, E Element) {Checkpositionindex(index);//Check whether it is out of bounds    if(index = = size)//Add at the end of the list        Linklast(element);Else                        //Add in the middle of the list        Linkbefore(Element,node(index));}void Linkbefore(e E, node<e> succ) {Finalnode<e> pred = succ.prev;FinalNode<e> NewNode =NewNode<> (Pred, E, succ); Succ.prev= NewNode;if(Pred = =NULL) First = NewNode;ElsePred.Next= NewNode;    size++; modcount++;}

From the source can be learned that
LinkedList when performing a query operation:
The first step: first to determine whether the element is close to the head, or near the tail.
Step Two: If you are close to the head, start by querying the head. Compared with ArrayList, of elementData(index) course, is a lot slower.

LinkedList the idea of inserting elements:
The first step: determine the position of the inserted element is the tail of the list, or the middle.
Step Two: If you add an element at the end of the list, point to the new node directly to the next pointer of the tail node.
The third step: if you add elements in the middle of the list, first determine whether the inserted position is the first node, then point the previous pointer to the new node. Otherwise, get the previous node (a) of the current node first, and point the next pointer of the a node to the new node, and then the next pointer to the new node points to the current node.

Vector

The data structure and use of vectors are similar to ArrayList. The biggest difference is that vectors are thread-safe. As can be seen from the following source code, almost all of the methods of data manipulation are modified by the Synchronized keyword. Synchronized is thread-synchronized, and when a thread has acquired a lock on a vector object, other threads must wait until the lock is released. From here we can learn that the performance of vectors is lower than ArrayList.
If you want a high-performance, thread-safe ArrayList, you can use the Collections.synchronizedList(list); method or use the Copyonwritearraylist collection

 Public synchronizedEGet(intIndex) {if(Index >= Elementcount)Throw NewArrayIndexOutOfBoundsException (index);return Elementdata(index);} Public synchronized Boolean Add(e) {modcount++;Ensurecapacityhelper(Elementcount +1); elementdata[elementcount++] = e;return true;} Public synchronized Boolean removeelement(Object obj) {modcount++;inti =indexOf(obj);if(I >=0) {Removeelementat(i);return true; }return false;}
Copyonwritearraylist

Here, let's take a quick look at the Copyonwrite container. It is a copy-on-write container. When we add an element to a container, instead of adding it directly to the current container, we copy the current container, duplicate a new container, then manipulate the element inside the new container, and finally point the reference of the original container to the new container. So the Copyonwrite container is a read and write separation of ideas, reading and writing different containers.
Application scenario: Suitable for high concurrency read operations (less read-write). If you write very many operations, you replicate the container frequently, which affects performance.

Copyonwritearraylist a copy of the collection when writing, when performing a write operation (such as: Add,set,remove, etc.), will copy the original array, and then do the modification on the new array. The reference to the last collection points to the new array.
Both copyonwritearraylist and vectors are thread-safe, and the difference is that the former uses the Reentrantlock class, which uses the Synchronized keyword. Reentrantlock provides more lock voting mechanisms that can perform better performance in the case of lock contention. Is that it allows the JVM to schedule threads more quickly and has more time to execute threads. This is why the performance of copyonwritearraylist is superior to vector in the case of large concurrent quantities.

PrivateEGet(Object[] A,intIndex) {return(E) A[index];} Public Boolean Add(e) {FinalReentrantlock lock = This.Lock; Lock.Lock();Try{object[] elements =GetArray();intLen = elements.length; object[] newelements = Arrays.copyOf(Elements, Len +1); Newelements[len] = e;SetArray(newelements);return true; }finally{lock.Unlock(); }}Private Boolean Remove(Object o, object[] snapshot,intIndex) {FinalReentrantlock lock = This.Lock; Lock.Lock();Try{object[] current =GetArray();intLen = current.length;        ...... object[] Newelements =NewObject[len-1]; System.arraycopy(Current,0, Newelements,0, index); System.arraycopy(Current, Index +1, Newelements, index, Len-index-1);SetArray(newelements);return true; }finally{lock.Unlock(); }}
Summarize

See here if the interviewer asks you what's the difference between ArrayList and LinkedList
If you answer: ArrayList query fast, write data slow, LinkedList query slow, write data fast. The interviewer can only count on your reluctance to pass.
If you answer: ArrayList query is fast because the bottom layer is implemented by the array, positioning the data by subscript fast. Write data is slow because copying an array is time consuming. LinkedList the bottom is a doubly linked list, the query data in turn slow traversal. Writing data simply modifies the pointer reference.
If you continue to answer: ArrayList and LinkedList are not thread-safe, small concurrency can use the vector, if there is a lot of concurrency, and read more write less can consider the use of copyonwritearraylist.
Because the Copyonwritearraylist bottom uses reentrantlock locks, the problem of lock contention is better handled than vectors using the synchronized keyword.
The interviewer will think you are a solid foundation, deep internal strength of talent!!!

Here the Java common List collection is finished using scene analysis. The last blog before the new year, a bit impetuous, may be in the workplace, the heart in the home! Finally I wish you a happy New Year!!! Lucky Dog!!! Big Rich big expensive!!! Maybe no one read the blog @@facesymbol@@‖∣ haha haha (⊙﹏⊙) Hiahiahia

Java Common List Collection usage scenario analysis

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.