The difference between java-arraylist and vectors

Source: Internet
Author: User

These two classes all implement the list interface (the list interface inherits the collection interface), they are all ordered collections, that is, the location of the elements stored in these two collections are sequential, equivalent to a dynamic array, we can later by the position index number to remove an element, And the data is allowed to be duplicated, this is the hashset, such as the maximum difference of the collection, such as hashset can not be indexed to retrieve the elements of the element, nor allow duplicate elements (originally the topic asked the hashset has no relationship, But in order to clarify the function of ArrayList and vector, we use the contrast method, which is more helpful to explain the problem.

Then the difference between ArrayList and Vector is mentioned, which mainly includes two aspects:. (1) Synchronization:
The vector is thread-safe, that is, it is thread-synchronized between its methods, and ArrayList is a thread-less program that is not synchronized between its methods. If there is only one thread routines access to the collection, it is best to use ArrayList, because it is more efficient than thread-safe, and if there are multiple lines routines access to the collection, it is best to use vectors, since we do not need to consider and write thread-safe code ourselves.

Note: For Vector&arraylist, Hashtable&hashmap, to remember the problem of thread safety, remember that the vector and Hashtable are old, Java is a birth to provide, they are thread-safe, ArrayList and HashMap are provided at JAVA2, and they are thread insecure. So, we talk about the old first class. (2) Data growth:
ArrayList and Vector have an initial capacity size, when the number of elements stored in them exceeds the capacity, you need to increase the storage space of ArrayList and vector, each time to increase storage space, not only add a storage unit, but add multiple storage units, Each increase in the number of storage units in memory space utilization and program efficiency to achieve a certain balance. Vector growth is twice times the default, and ArrayList's growth strategy is not clearly defined in the documentation (from the source code to see the growth of 1.5 times times the original). Both the ArrayList and vector can set the initial space size, and the vector can also set the amount of space to grow, while ArrayList does not provide a way to set the growth space.
Summary: That is, the vector increases the original one times, the ArrayList increases the original 0.5 times times.

The difference between java-arraylist and vectors

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.