1. PairListSelect
Both arraylist and sorted list implement the list interface, and the vector is also, but it is no longer used. Arraylist is preferred.
2.Set,
You can select treeset, hashset, or javashashset. The performance of hashset is always better than that of treeset (especially the most common operations for adding and querying elements ). The only reason treeset exists is that it can maintain the sorting status of elements. Therefore, treeset should be used only when you need a sorted set.
NOTE: For the insert operation, the linkedhashset is slightly slower than the hashset; this is caused by the maintenance of the linked list.
Additional overhead. However, with the linked list, traversing the javashashset will be faster.
3.MapSelect
When selecting different map implementations, the size of map is the most important factor affecting the performance,
The efficiency of hashtable and hashmap is roughly the same. (As you can see, hashmap
It is usually faster, so hashmap intentionally replaces hashtable .) Treemap is generally slower than hashmap,
Why do we still need it? It can be used to generate an ordered queue. The behavior of the tree is as follows:
Sorting status, no special sorting operation is required. After the treemap is filled, you can call keyset (),
Obtain the set composed of "keys" and generate an array of "keys" using toarray. Next, use static
Arrays. binarysearch () method (discussed later) to quickly query objects in sorted arrays. Of course,
You should do this only when hashmap cannot be used for some reason. Because hashmap is
It is designed for fast query. In addition, you can easily generate a hashmap through treemap. Therefore, when
Hashmap is preferred when you need map. It is used only when you need a map that is always sorted.
Treemap.
Linkedhashmap is a little slower than hashmap because it maintains the hash data structure while maintaining the linked list.
Identityhashmap has completely different performance because it uses = instead of equals () to compare elements.
Java container summary:
1. arrays associate numbers with objects. It stores objects with clear types and does not need
The result is converted to a type. It can be multidimensional and can save basic data types. However, the array
Once generated, the capacity cannot be changed.
2. Collection stores a single element, while MAP stores the associated key-value pairs.
3. Like an array, list also establishes the association between numbers and objects. It can be considered that arrays and lists are sorted in order.
. List can automatically expand the capacity. However, the list object type cannot be saved. Only objects can be saved.
Therefore, the type conversion must be performed on the results of the objects retrieved from the container.
4. If you want to perform a large number of random access, use arraylist; if you want to insert or
To delete an element, use the sequence list.
5. queue, bidirectional queue, and stack behavior are supported by the queue list.
6. Map is a design that associates objects with objects. Hashmap focuses on fast access; treemap
Keep the "key" in the sorting state, so there is no hashmap fast. Linkedhashmap Protection
You can use the LRU algorithm to re-Sort elements in the insert sequence.
7. Set does not accept repeated elements. Hashset provides the fastest query speed, and treeset keeps elements in the row
Status. Linkedhashset stores elements in the insert sequence.
8. The outdated vector, hashtable, and stack should not be used in the new program.