Comparison: HashMap and TreeMap

Source: Internet
Author: User

1. AbstractMap abstract class and SortedMap Interface

AbstractMap abstract class: overwrites the equals () and hashCode () methods to ensure that two equal mappings return the same hash code. If the two mappings are of the same size, contain the same key, and each key has the same value in the two mappings, the two mappings are equal. The mapped hash code is the sum of the mapped element hash codes. Each element is an implementation of the Map. Entry interface. Therefore, two equal mappings report the same hash code regardless of the internal order of the ing.

SortedMap interface: used to maintain the orderly order of keys. The SortedMap interface is the image view (subset), including two endpoints that provide access methods. Except for sorting keys acting on mappings, SortedMap processing is the same as SortedSet processing. The elements added to the SortedMap implementation class must implement the Comparable interface. Otherwise, you must provide a Comparator interface for its constructor. The TreeMap class is its only implementation.

2. Two conventional Map implementations (inherited from AbstractMap and SortedMap respectively)

HashMap: implemented based on hash tables. The key classes required to be added using HashMap clearly define hashCode () and equals () [hashCode () and equals ()]. To optimize the use of HashMap space, you can optimize the initial capacity and load factor.

(1) HashMap (): Construct an empty hash image.

(2) HashMap (Map m): Build a hash image and add all Mappings of the image m.

(3) HashMap (int initialCapacity): creates an empty hash image with a specific capacity.

(4) HashMap (int initialCapacity, float loadFactor): Build an empty hash image with a specific capacity and loading Factor

TreeMap: implemented based on the red and black trees. TreeMap has no optimization option because the tree is always in the balance state.

(1) TreeMap (): Construct an empty image tree

(2) TreeMap (Map m): build an image tree and add all elements in image m.

(3) TreeMap (Comparator c): build an image tree and use a specific Comparator to sort keywords.

(4) TreeMap (SortedMap s): build an image tree, add all mappings in the image tree s, and sort by the same comparator as the ordered image s.

3. Two common Map performances

HashMap: applies to inserting, deleting, and locating elements in a Map.

Treemap: Applicable to traversing keys in natural or custom order ).

4. Summary: HashMap is usually a little faster than TreeMap (because of the data structure of the tree and hash table). We recommend that you use HashMap to use TreeMap only when you need to sort the Map.

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.