The difference between HashMap and TreeMap in Java Deep Understanding (reprint)

Source: Internet
Author: User

let's start by describing what a map is. In the array we index the content by the array subscript, and in the map we index the object by object, and the object to be indexed is called


to do a key, its corresponding object is called value. This is what we call a key-value pair.


HashMap quickly find its content through hashcode, and all elements in TreeMap remain in a fixed order, and if you need to get an orderly result you should


this uses treemap (the order of the elements in the HashMap is not fixed).
HashMap Non-thread-safe TreeMap non-thread safe


Thread Safety
in Java, thread safety generally manifests itself in two ways:
1, multiple thread access to the same Java instance (read and modify) does not interfere with each other, it is mainly embodied in the keyword synchronized. such as ArrayList and vectors,


HashMap and Hashtable
(the latter has synchronized keywords before each method). If you are interator a list object and the other threads remove an element, the problem arises.


2. Each thread has its own field and is not shared across multiple threads. It is mainly embodied in the Java.lang.ThreadLocal class, without the Java keyword support, such as Static


, transient.
1.AbstractMap abstract class and SortedMap interface
Abstractmap abstract class: (HashMap inheritance Abstractmap) overrides the Equals () and Hashcode () methods to ensure that two equality mappings return the same hash code. If two mappings


The two mappings are equal if they are equal in size, contain the same key, and each key has the same value in both maps. The hash code of the map is the sum of the hash codes of the mapped elements, each of which


an element is an implementation of the Map.entry interface. As a result, two equality mappings report the same hash code regardless of the internal order of the mappings.
SortedMap Interface: (TreeMap inherits from SortedMap) it is used to keep the key in an orderly order. The SortedMap interface is a view (subset) of the image, including two endpoints providing access to the


law. In addition to sorting is a key that works on a map, processing SortedMap is the same as dealing with SortedSet. Elements added to the SortedMap implementation class must implement the comparable interface, no


then you must provide an implementation of the comparator interface to its constructor. The TreeMap class is its only implementation.


2. Two general map implementations
HashMap: Based on hash table implementation. The key class that is added with HashMap requires a clear definition of hashcode () and Equals () [can override Hashcode () and Equals ()], in order to optimize


with the use of HashMap space, you can tune the initial capacity and load factor.
(1) HashMap (): Build an empty hash map image
(2) HashMap (map m): Build a hash map image and add all the mappings for the images m
(3) HashMap (int initialcapacity): Build an empty hash map image with a specific capacity
(4) HashMap (int initialcapacity, float loadfactor): Constructs an empty hash map image with a specific capacity and load factor
TreeMap: Based on red-black tree. TreeMap has no tuning option because the tree is always in equilibrium.
(1) TreeMap (): Building 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 sort the keywords using a specific comparer
(4) TreeMap (SortedMap s): Build an image tree, add all the mappings in the image tree S, and sort with the same comparer as the ordered image s


3. Two General map performance
HashMap: Applies to inserting, deleting, and locating elements in a map.
Treemap: For traversing keys (key) in natural order or in a custom order.


4. Summary
HashMap is usually faster than treemap (the data structure of the tree and hash table), it is recommended to use HashMap, and TreeMap is used when the map needs to be sorted.

The difference between HashMap and TreeMap in Java Deep Understanding (reprint)

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.