We’ve focused on ArrayLIst in the last few articles, and it’s time to release this diagram.

The content of this figure for us to learn Java, is very important, the white part is needed to understand, the yellow part is we want to focus on understanding, not only to know how to use, at least also need to read the source code. The green section is rarely used anymore, but it might be asked in an interview question. Let’s look at a common interview question: What is the difference between an Arraylist and a Vector?

First we give the standard answer:

Vector is thread-safe. ArrayList is not thread-safe.

ArrayList is 0.5 times larger when the underlying array is insufficient. Vector is 1 times larger.

Vector, like ArrayList, is derived from List

Implements the List interface, and the underlying ArrayList, are arrays to achieve. Take a look at the add methods of each class, starting with the Add source for ArrayList

Take a look at the add source for Vector

Methods are implemented the same way, except that they add the synchronized keyword. Let’s look at the other methods

Look again at the remove method on Vector

The same applies to the synchronized keyword, but look at the ArrayList get method

Get method on Vector

Let’s look at the other methods of Vector

Without exception, as long as it is a critical operation, methods are preceded by the synchronized keyword, to ensure the safety of the thread. When a synchronized modified method is executed, the system will add a lock to the method and release the lock after the method is executed. The process of adding and releasing the lock is expensive in the system. Therefore, in a single-threaded environment, Vector efficiency is much lower. (Multithreaded environments do not allow ArrayList, need to do).

As for the underlying array expansion difference, here is not with you to read the source code, interested friends we read it, the underlying code is almost the same, the difference is only the length of the new array after calculation is inconsistent.

Like ArrayList and Vector, the same classes have similar relationships: HashMap and HashTable, StringBuilder and StringBuffer, which are thread-safe implementations of ArrayList and Vector. Hope everyone in the interview process, can say a because so, rather than blindly to the back of the test, only understanding, no need to back.

Note: As for thread safety, this is just a brief description of the differences between the two classes.

Previous: ArrayList time complexity – Java things column

Next: Linear tables for Java Data structures – Java stuff


Note: This column was first published on the public account saysayJava. All sample codes have been uploaded to the public account, please pay attention to download.

If you liked this series, please give me a thumbs up or share it with me. Your support is what keeps me going, and leave a comment in the comments section where you can find out what you want to know about this column.

Reprint unlimited welcome, but please indicate “author” and “original address”. Please keep this paragraph in the article, thank you for your respect for the author’s copyright. For commercial reprint or publication, please contact the author for authorization