In use
Java
When using (
Collection
),
Java API
It provides multiple sets of implementations, Which I frequently encounter during use and interviews.
"
Choice
"
.
:
) (Mainly during the interview)
Over time, I had a little bit of experience and wrote it for discussion.
.
In general,
Java API
All the collection classes used in
Collection
Interface. Its class inheritance structure is as follows:
Collection <-- list <-- Vector
Collection <-- list <-- arraylist
Collection <-- list <-- items list
Collection <-- set <-- hashset
Collection <-- set <-- hashset <-- revoke hashset
Collection <-- set <-- sortedset <-- treeset
Vector:
Based on
Array
Of
List
Is encapsulated.
Array
Some of the features we don't have are easy to use.
Array
. Performance cannot surpass
Array
. Therefore, when possible, we need to use more
Array
. Another important point is
Vector "sychronized"
This is also
Vector
And
Arraylist
The only difference.
Arraylist
: Same
Vector
The same is based on
Array
But the difference is that
Arraylist
Not synchronous. Therefore, the performance is better
Vector
It is superior, but when running in a multi-threaded environment, you need to manage the thread synchronization issues on your own.
Shortlist
:
Shortlist
Different from the previous two types
List
, It is not based on
Array
, So do not
Array
Performance constraints. Each of its nodes (
Node
) Includes two aspects:
1.
Node data (
Data
);
2.
Information of the next node (
Nextnode
). So when
Shortlist
To delete an action, you do not need
Array
Of
List
Similarly, a large amount of data must be moved. Change
Nextnode
You can achieve this. This is
Shortlist
.
List
Summary:
1.
All
List
Can only contain tables composed of a single object of different types, rather
Key
-
Value
Key-value pairs. For example:
[Tom, 1, C]
;
2.
All
List
Can have the same elements, such
Vector
Can have
[Tom, koo, too, koo]
;
3.
All
List
Can have
Null
Element, such
[Tom, null, 1]
;
4.
Based on
Array
Of
List
(
Vector
,
Arraylist
) Is suitable for queries, while
Shortlist
(Linked list) is suitable for adding and deleting operations.
Hashset
: Although
Set
Same
List
All are implemented
Collection
But their implementation methods are quite different.
List
Basically
Array
. However
Set
In
Hashmap
This is
Set
And
List
.
Hashset
The storage method is
Hashmap
In
Key
As
Set
. Look
Hashset
Of
Add
(
Object OBJ
.
Public Boolean add (Object OBJ)
{
Return map. Put (OBJ, present) = NULL;
}
This is why
Set
Cannot be
List
Is the root cause of repeated items because
Hashmap
Of
Key
There cannot be duplicates.
Linkedhashset
:
Hashset
A subclass, a linked list.
Treeset
:
Sortedset
It is different from
Hashset
Is
Treeset
It is ordered. It is through
Sortedmap
.
Set
Summary:
1. Set
The basis of implementation is
Map
(
Hashmap
);
2. Set
The elements in cannot be repeated.
Add (Object OBJ)
Method to add an existing object, it overwrites the previous object;