Java collection in the selection of the time should choose that appropriate? A table that tells you how to choose a Java collection
|
Java collection classes |
is thread safe |
Whether the key value pair |
Sort |
Performance |
Whether the element can be duplicated |
Scaling strategy |
List interface |
ArrayList |
Whether |
Whether |
Maintain the order of the time |
High query performance, and the performance of adding and deleting elements is low |
Is |
1.5 times-fold expansion with an element greater than 100% of the current capacity |
LinkedList |
Whether |
Whether |
Have order |
Low query performance, and high performance of elements and additions |
Is |
|
Vector |
Is |
Whether |
Have order |
Low |
Is |
|
Stack |
Is |
Whether |
Have order, advanced after out |
Low |
Is |
|
Map interface |
HashMap |
Whether |
Is |
Not sorted |
Quick Insert and find |
Key cannot be duplicated, value can be duplicated |
Twice-fold expansion with an element greater than 75% of the current capacity |
Hashtable |
Is |
Is |
Not sorted |
Low |
Key cannot be duplicated, value can be duplicated |
|
TreeMap |
Whether |
Is |
Auto Sort by key |
Quick Insert and find |
Key cannot be duplicated, value can be duplicated |
|
Set interface |
HashSet |
Whether |
Whether |
Not sorted |
Quick Insert and find |
Whether |
|
TreeSet |
Whether |
Whether |
Default ascending sort (return value by compare to interface) |
Quick Insert and find |
Whether |
|
User guide:
1 in the case of the set scenario, specify the initialization capacity, because the capacity is not specified, the initial capacity is 10, each expansion will be a copy of the underlying array, consumption performance;
2 All sets the underlying storage is an array, the array can hold the base type, and the collection can only save objects
The 3 Java.lang.reflect.Array tool provides an array of operations; The Java.util.collection tool provides a collection of operations
4 HashMap can be synchronized with map M = Collections.synchronizedmap (HASHMAP)
Java Common Collection Selection guide