Differences between vector arraylist sorted list in Java

Source: Internet
Author: User

In the past, I often encountered these problems when I used to write questions. Sometimes I don't understand them very well. I found a good summary post on the Internet today and posted it to share it with you ~~~~~~

In Java, vector arraylist objects list:

Summary
If operations such as stacks and queues are involved, you should consider using the list. For elements that need to be inserted and deleted quickly, you should use the random list. If you need to quickly access elements randomly, you should use the arraylist.
If the program is in a single-threaded environment or the access is only performed in one thread, the efficiency of non-synchronous classes is high. If multiple threads may operate on one class at the same time, synchronous classes should be used.
Pay special attention to the operations on the hash table. The equals and hashcode methods should be correctly rewritten as the key object.
Try to return the interface rather than the actual type. For example, if the list is returned rather than the arraylist, the client code does not need to be changed if you need to replace the arraylist with the explain list later. This is for abstract programming.

Synchronization
The vector is synchronized. Some methods in this class ensure that the objects in the vector are thread-safe. Arraylist is asynchronous, so the objects in arraylist are not thread-safe. Because the synchronization requirements will affect the execution efficiency, it is a good choice to use arraylist if you do not need a thread-safe set, this avoids unnecessary performance overhead due to synchronization.
Data Growth
In terms of the internal implementation mechanism, both arraylist and vector use arrays to control objects in the set. 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, and arraylist is 50% of the original length. Therefore, the space occupied by this set 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.
Usage mode
In arraylist and vector, it takes the same time to search for data from a specified position (through an index) or add or remove an element at the end of the set, this time is represented by 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? It is assumed that all elements after the I and I elements in the collection must be displaced during the above operations. What does all this mean?
This means that you can only search for elements at a specific position or add or remove elements at the end of the set. You can use vector or arraylist. For other operations, you 'd better select another set operation class. For example, does the linklist set class take the same time to add or remove any element from the set? O (1), but it is slow to index an element-O (I), where I is the index position. it is also easy to use arraylist, because you can simply use indexes instead of creating iterator objects. Linklist also creates an object for each inserted element, and you need to understand that it also brings additional overhead.
Finally, in practical Java, Peter Haggar recommends using a simple array instead of vector or arraylist. This is especially true for programs with high execution efficiency requirements. Array is used to avoid synchronization, additional method calls, and unnecessary Space reallocation.

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.