preface

I’m sure many of you know the set framework List, but do you really know the List? Are some concepts still a little vague? Then you might as well read my article, maybe you will get something!

Parsing the ArrayList

A: First, we’ll look at the ArrayList collection, which is a collection we use a lot. The bottom of an ArrayList is an array, and an ArrayList has the concept of scaling up, and because it can scale up, it can grow “dynamically.” ArrayList is thread-unsafe. (Note: interested students can have a look at the JDK source code, read the source code for your programming ability will be a great improvement)

Parsing the Vector

A: Vector is a JDK1.2 class. It is an older collection class that is rarely used today. Vector is also an array. The biggest difference from ArrayList is thread-safety.

Parsing LinkedList

A: The underlying LinkedList is a two-way list that is thread-unsafe.

What is the difference between Vector and ArrayList? What are the respective usage scenarios?

A: ArrayList: The underlying implementation is array, which is not thread-safe. Queries and modifications are very fast, but additions and deletions are slow.

LinkedList: The underlying LinkedList is a two-way list, which is not thread-safe and is slow to query and modify, but fast to add and delete.

Vector: The underlying array implementation is thread-safe, and synchronized is used for locking operations.

Usage scenarios: Vector is rarely used anymore, LinkedList is used for adding and deleting scenes, and ArrayList is used for querying and modifying scenes.

What do you do if you make ArrayList thread-safe?

A: There are three ways to introduce this question:

The first: write a package class to lock operations, generally according to your business custom.

The second: use a List < String > List = Collections. SynchronizedList (new ArrayList < > ()); To create an ArrayList that is thread-safe. You can see from the JDK source that this method uses synchronized for locking.

Third way: use CopyOnWriteArrayList<>(); It is a class under the java.util.Concurrent package. This method uses ReentrantLock to lock.

CopyOnWriteArrayList and Collections. SynchronizedList realize thread safe and what’s the difference? What are the usage scenarios?

Answer: CopyOnWriteArrayList: Modify operation, will copy a new array for operation (add, set, remove, etc.), the price is very expensive, after the implementation of the modification of the original collection to the new collection to complete the modification operation, source code with ReentrantLock ReentrantLock to ensure that there will not be multiple threads at the same time copy a number of groups.

Usage scenario: This parameter is applicable to scenarios where read operations are much larger than write operations. Locks are not required for read operations, but for direct obtain, delete, and add operations.

Collections. SynchronizedList: thread-safe because it almost in each method USES a synchronized synchronous locking mechanism.

Scenario: The write performance is better than CopyOnWriteArrayList, but the read performance is not.

What are the disadvantages of CopyOnWriteArrayList?

A: Memory usage problem, write copy mechanism, memory will be stationed in two objects at the same time.

Expansion mechanism for ArrayList

A: The default size of ArrayList is 10 before JDK1.7 and 0 after JDK1.7. If the size of the collection is not specified, the default value is 0, and if the size is specified, the collection size is the specified size. When the collection first adds elements, the collection is expanded to 10. When the number of elements in the ArrayList is greater than its capacity, the expanded size = original size + original size /2. For example, when you add the first element, the set size increases to 10; When you add the 11th element, the set size increases to 15.

conclusion

About the collection framework List, the above content is what I know, welcome to continue to add. Specific methods they have, I suggest to look at the JDK source. Of course, just knowing the concept is not enough. To really grasp it, you need to use it in real projects.