List. Set. Map and Java container summary of the three container interfaces in Java

Source: Internet
Author: User

 

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.