Java performance (4) vector, arraylist, sorted list

Source: Internet
Author: User

I remember the difference between arraylist and vector I was asked during each interview. Although I knew what I was trying to answer each time, I always felt that I was not very familiar with it. So I would like to summarize the information.

JDK has several sorted collections of Java. util. list implementation methods. The three most familiar ones are vector, arraylist, and sorted list. in terms of Java performance, the performance difference between the three lists is often described.

Both vector and arraylist are stored element-based objects []
Array to access elements based on indexes. The biggest difference between vector and arraylist is the use of synchronization,
Aside from the two methods used only in the serialization process, none of the arraylist methods are synchronous. On the contrary, most of the vector methods are directly or indirectly synchronized, because
In this case, the vector is thread-safe, and the arraylist is not. As a result, the arraylist is faster than the vector. However, in the latest JVM, the speed difference between the two classes
Is small and almost negligible.

The implementation of vector and arraylist has excellent performance in index access and element update, because it does not require effort to perform range check (range
Therefore, adding elements from the end of Lise and deleting elements also have excellent performance, unless the storage capacity of the List is insufficient and the length of the internal array needs to be extended. In this case, insert
An array is required for data entry and deletion (two copies are required when the internal array needs to be extended ), the number of elements to be copied is the length of the list minus the index number (that is, the index number to be inserted and deleted ).
With the last index number of the entire list), for insertion, inserting an element into the first position of the list has the worst performance, and inserting to the last position has the best performance, the time required for copying an array is
The number of elements increases significantly.

Implementation of tranquility list

The lateral list is implemented by using a list of two-way link nodes. when accessing elements based on indexes, you need to access all nodes only to the index node you want. It is very easy to insert elements into the sortedlist. You only need to insert a node directly before the node to reach the index node.

The implementation of the optimize list makes the performance of accessing elements and updating elements based on indexes inferior to that of arraylist and vector, because you need to access many nodes to reach the node you want. The distance between the inserted and deleted elements and the inserted and deleted node points to the end of the list is the maximum.

Performance Comparison:

For a short list, the performance of arraylist and vector is very similar. If you only need a fixed-length list, the performance of Java array (ojbect []) exceeds any other list type.

In general, arraylist has better performance in most cases, including inserting elements based on the index or inserting them to the right seat. If you always want to insert data to the front end of the list, linkedlist has the best performance, but you should be able to use anti-arraylist for better performance.

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.