Vector & arraylist__ Deep Java collections

Source: Internet
Author: User

In writing Java, I basically like to use ArrayList, even I do not know that there is a vector. Check that the discovery is a thread-safe issue ... Why did thread safety revolves around me every day ... Alibaba, let me start to understand Java's so-called thread safety issues.

The following is from:

1. Vector & ArrayList
1 Vector method is synchronous (Synchronized), is thread safe (thread-safe), and ArrayList method is not, because the synchronization of threads must affect performance, therefore, ArrayList performance than vector better.
2 when the vector or ArrayList element exceeds its initial size, the vector doubles its capacity, while the ArrayList only increases by 50%, so that ArrayList is useful for saving memory space.

2. Hashtable & HashMap
Hashtable and hashmap their performance comparisons are similar to vectors and ArrayList, such as the Hashtable approach is synchronous, and HashMap is not.

3. ArrayList & LinkedList

ArrayList's internal implementation is based on an internal array object[], so conceptually it's more like an array, but LinkedList's internal implementation is based on a set of connected records, so it's more like a list structure, so there's a big difference in performance:
From the above analysis, we know that when inserting data in front or in the middle of ArrayList, you have to move all subsequent data backwards, which will take a lot of time, so when you add data to a column of data instead of in front or in the middle, and you need to randomly access the elements, The use of ArrayList will provide better performance; When you access an element in a linked list, you have to look at one end of the list along the direction of the link, until you find the element you want, so when you add or delete data in front of or in the middle of a column of data, and to access the elements in order, you should use LinkedList.

If in the programming, 1, 22 kinds of situations alternately appear, at this time, you may consider uses the list such universal interface, but does not care about the concrete implementation, in the concrete situation, its performance is guaranteed by the concrete implementation.

4. Configure the initial size of the collection class
The size of most classes in the Java Collection framework can be increased correspondingly as the number of elements increases. We don't seem to care about its initial size, but if we consider the performance of a class, it's important to consider setting the initial size of the collection object as much as possible, which can greatly improve the performance of the code.
For example, the default initial size of Hashtable is 101, and the load factor is 0.75, which means that if there are more than 75 elements, it must increase the size and rearrange the elements, so if you know that the exact number of elements is 110 when you create a new Hashtable object, Then, you should set its initial size to 110/0.75=148 so that you can avoid the need to rearrange the memory and increase the size.

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: 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.