Relationship and difference between arraylist, vector list, and vector

Source: Internet
Author: User

The arraylist, inclulist, and vestor classes all implement the java. util. List interface, but they have different features, mainly as follows:

I,Synchronization

Arraylist and inclulist are not synchronized, while vestor is. Therefore, if thread security is required, arraylist or javaslist can be used to save costs for synchronization. But in the case of multithreading, sometimes we have to use vector. Of course, you can also package arraylist and history list in some ways to synchronize them, but the efficiency may be reduced.

II,Data Growth
In terms of the internal implementation mechanism, both arraylist and vector are stored in the objec array format. When you add elements to these two types, if the number of elements exceeds the current length of the internal array, both of them need to extend the length of the internal array,By default, vector automatically doubles the length of the original array. arraylist is 50% of the original length.In the end, the space occupied by the collection is always larger than what you actually need. Therefore, if you want to save a large amount of data in the collection, using vector has some advantages, because you can avoid unnecessary resource overhead by setting the initialization size of the collection.

III,Efficiency of searching, inserting, and deleting objects

In arraylist and vector, an object is retrieved from the specified position (using index), orEndThe time for inserting or deleting an object is the same, which can be expressed as O (1 ). However, if an element is added or removed from another position in the Set, the time consumed will grow linearly: O (n-I), where N represents the number of elements in the set, I indicates the index location where the element is added or removed. Why? When performing the preceding operations, all the elements after the I and I elements in the set must perform the (n-I) object displacement operation.

In the collections list, in the insert or delete setAny locationThe time spent by the element is the same-O (1), but it is slow when indexing an element, which is O (I), where I is the index position.

Therefore, if you only search for elements at a specific position or add or remove elements only at the end of the set, you can use vector or arraylist. If you want to insert or delete other operations at the specified position, you are advised to select the recommended list.

 

 

References:

Http://www.matrix.org.cn/resource/article/2003-12-16/745.html

 

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.