java-tip-the difference between various maps and how to choose

Source: Internet
Author: User
Here are the main discussion of these three kinds: HashMap, Linkedhashmap, TreeMap 1. HashMap is a regular hash table, query and insert the best performance, if there is no special requirements, should use this

2. Linkedhashmap: This class is interesting, it can specify the order of traversal: 1) The order in which elements are added 2) in the least recently used order

The implementation of this class inherits HashMap, just more than HashMap maintains a doubly linked list, so its query and insert performance is slightly worse than HashMap. But its traversal performance is better than HashMap.

use Linkedhashmap If you want to traverse in order of element additions.

3. TreeMap, the internal implementation of this class is similar to the C + + std::mapTreeMap The element is stored in a red-black tree, the key stored in it must implement the comparable interface. Because of the use of red-black trees, the key stored in it is from small to large sequence. Its performance indicators are worse than hashmap.

If you want the elements in the map to be ordered, use TreeMap.

java-tip-the difference between various maps and how to choose

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.