Talking about the difference between ArrayList---LinkedList---Vector in java collection


Preface

Containers are an indispensable part of daily development. Java has encapsulated a lot of containers for me. We can use them to greatly improve our development efficiency. However, if we don’t know the principle, we will use it and it may not be good. In line with the principle of good use (to improve the efficiency of the system). Knowing it, but also knowing why. Let's introduce the difference between ArrayList-LinkedList-Vector and the daily application.


Tip: The following is the content of this article, the following cases are for reference

1. Definition

They all implement the List interface, so the specific functions are relatively similar. For example, they all provide positioning, adding or deleting operations based on location, and they all provide iterators to traverse their content. But because of specific design differences, the performance is quite different in terms of behavior, performance, thread safety, etc.
Vector is a thread-safe dynamic array provided in the early days of Java. If thread safety is not required, it is not recommended. After all, synchronization has additional overhead. Vector uses an array of objects to store data. The capacity can be automatically increased as needed. When the array is full, a new array will be created and the original array data will be copied. (The bottom layer is an array implementation)

ArrayList is a more widely used dynamic array implementation. It is not thread-safe by itself, so the performance is much better. Similar to Vector, ArrayList can also adjust the capacity according to needs, but the adjustment logic of the two is different. Vector will be doubled when expanding, while ArrayList will be increased by 50%. (The bottom layer is an array implementation)

LinkedList, as the name suggests, is a doubly linked list provided by Java, so it does not need to adjust the capacity like the above two, and it is not thread-safe. (The bottom layer is a linked list implementation)

Two, expansion

1. Keywords

Internal sort, external sort, array, linked list

1. Efficiency

The LinkedList is much more efficient for node insertion and deletion, but the performance of random access is slower than that of dynamic arrays.
Vector and ArrayList are dynamic arrays, and their internal elements are stored sequentially in array form, so they are very suitable for random access occasions. In addition to inserting and deleting elements at the tail, performance is often relatively poor. For example, when we insert an element in the middle position, we need to move all subsequent elements.

The Collection interface is the root of all collections, and then expanded to provide three types of collections, namely:

List, which is the most ordered collection we introduced earlier, provides convenient access, insertion, and deletion operations.

Set and Set do not allow duplicate elements. This is the most obvious difference from List, that is, there are no two objects equals return true. We have many occasions where we need to ensure the uniqueness of elements in our daily development.

Queue/Deque is the implementation of the standard queue structure provided by Java. In addition to the basic functions of the collection, it also supports similar first-in-first-out (FIFO, First-in-First-Out) or last-in first-out (LIFO, Last-In) -First-Out) and other specific behaviors. BlockingQueue is not included here, because it is usually a concurrent programming occasion, so it is placed in the concurrent package.

The general logic of each set is abstracted into the corresponding abstract class. For example, AbstractList concentrates the common parts of various List operations. These collections are not completely isolated. For example, LinkedList itself is both a List and a Deque.

If you read more source code, you will find that, in fact, the TreeSet code is actually implemented by TreeMap by default. The Java class library creates a Dummy object "PRESENT" as the value, and then all inserted elements are actually put in the form of keys. Into the TreeMap; in the same way, HashSet is actually implemented on the basis of HashMap. It turns out that they are just the vest of the Map class!

As mentioned earlier, we need to implement various specific collections, at least understand the basic features and typical usage scenarios, take several implementations of Set as examples:

TreeSet supports natural sequential access, but operations such as addition, deletion, and inclusion are relatively inefficient (log(n) time).

HashSet uses a hash algorithm. Ideally, if the hash is normal, it can provide constant-time addition, deletion, and inclusion operations, but it does not guarantee order.

LinkedHashSet internally builds a doubly linked list of record insertion order, so it provides the ability to traverse in the order of insertion. At the same time, it also guarantees constant-time addition, deletion, and inclusion operations. These operations have slightly lower performance than HashSet because Need to maintain the overhead of the linked list.

When traversing elements, the performance of HashSet is affected by its own capacity, so when initializing, unless it is necessary, do not set the capacity of the HashMap behind it too large. For LinkedHashSet, due to the convenience provided by its internal linked list, the traversal performance only depends on the number of elements.

to sum up

1 Use an array for more insertions
2 Use a linked list for more deletions

Guess you like

Origin blog.csdn.net/aa327056812/article/details/109528198