Tips for selecting collection classes in Java

Source: Internet
Author: User

Tips for selecting collection classes in Java

JDK APIs define a variety of collection classes, but the difficulty lies in how to select appropriate classes based on specific requirements. Here, we describe some tips for selecting collection classes:

1. If it is not limited to JDK 1.1.x, general implementation classes in the collection structure should be used instead of basic classes such as vector, stack, and hashtable.

2. The arraylist class should be used for an ordered collection structure (ordered, which already has an ordered structure, but you do not want this structure to be disrupted) instead of a vector.

3. We recommend that you use hashset for unordered collection structures (that is, structures with random data order) to make full use of the query efficiency.

4. Key-value pairs use hashmap instead of hashtable for associated data, because the latter has more powerful operation performance.

5. If the random access performance requirement for each element is higher than the execution speed requirement for inserting or removing elements in the list, we recommend that you select arraylist to access the elements in the structure by index.

6. If the performance requirements for first-end access and removal efficiency are higher than those for immediate access, we recommend that you use the shortlist to quickly locate the first-end access and insert and remove access.

7. It is recommended that treemap be used to repeat all elements in the key-value Association relation group with the keywords in order (iterator, which contains the traversal meaning) without disrupting the order.

8. Use treeset to save a group of key-value pairs sequentially.

9. To ensure data consistency when thread security is required, use the synchronization constraints provided by the collection class. Do not use synchronization constraints when synchronization is not required to avoid overhead.

Related Article

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.