1. JavaSE _ Collection (simple Collection, Collection, List) and javasecollection
Set (1)
1. simple understanding of Collections
1. Ridicule
Collections, to put it bluntly, are data storage. Remember that in OOP, we once said that OOP puts data first and then considers the algorithm for operating data. That is to say, we will not talk about the data structure. A set is used to store data. There are different ways to store data, such as arrays and collections. What is the difference between a set and an array?
|
Differences |
Array |
1. Arrays can store both basic data types and reference data types. Basic data types store values and Reference Data Types store address values. 2. the array length is fixed and cannot be automatically increased. |
Set |
1. A collection can only store referenced data types (objects), and can also store basic data types in a collection. However, it is automatically packed into objects during storage. 2. The length of the set is variable and can increase according to the increase of elements. |
After reading this, you must know when to use arrays and when to use collections ~
Array: When elements are fixed, we recommend that you use arrays.
Set: When the elements are not fixed, the set is recommended.
2. A preliminary understanding (Collection Architecture)
Here comes the dish ,:
The figure shows the characteristics of each set. Just as we use different tools during cooking, different bowls and dishes have different effects.
Next we will focus on List.
Collection is the root interface of a single column set. In the figure, there are two subinterfaces. One is List and the other is Set. Today, we mainly analyze the List, that is, ArrayList, rule List, and Vector ).
List is a set of sequential storage, which can be stored repeatedly. Let's take a look at this code: List list = new ArrayList (); Note: generics are not considered here. Let's skip this step.
The List interface points to the specific implementation of sub-classes, and ArrayList stores ordered sets, which will increase with the increase of storage elements. Similarly, the same is true for 'arraylist'. What is the difference between 'arraylist' and 'arraylist?
If you want to know the differences, you must know how they are implemented at the underlying layer? Format the figure above.
ArrayList |
The underlying layer is implemented using arrays. |
Shortlist |
Underlying implementation using linked lists |
One is array implementation, and the other is linked list implementation.
What are the characteristics of an array? The query speed of an array is fast, because the query speed is one step in place based on the index; the increase and deletion speed is slow, because the index must be moved when the deletion is added.
What are the features of a linked list: linked list deletion and addition are accelerated, but query and modification are slow.
Array |
* Fast query and Modification *Added and deleted slowly |
Linked List |
* Slow query and Modification *Added and deleted quickly |
Ii. Features of three subclasses of the List set
1.
|
Features |
ArrayList |
The underlying data structure is an array, slow query, and fast addition and Deletion Low thread security and High Efficiency
|
Shortlist |
The underlying data structure is a linked list, which is slow in query and fast in addition and deletion. The thread is not secure and the efficiency is high. |
Vector |
The underlying data structure is an array, which provides fast query and slow addition and deletion. Thread security and low efficiency. |
2.
Slow query of Vector Against ArrayList (thread-safe)
Slow addition/deletion of a Vector relative to the sorted list (array structure)
3. Differences between Vector and ArrayList:
Vector is thread-safe with Low Efficiency
ArrayList is thread unsafe and efficient.
In common:ArrayImplemented
4. Differences between ArrayList and rule list:
The bottom layer of ArrayList is the array result, which is fast to query and modify.
The bottom layer of the shortlist is the linked list structure, with fast addition and deletion and slow query and modification.
In common:Thread SecurityOf
5. Since List has three subclasses, how should we choose?
Multiple queriesUse ArrayList
Add, delete, and moreUse tranquility list
If there are manyArrayList (most commonly used)
If any error occurs, please correct it.
Email: it_chang@126.com