The differences and connections between TreeMap and HashMap in Java:

Source: Internet
Author: User


1. What is a map?
In the data structure, map is a data structure model which is saved by a set of key-value pairs (key,value).

2. The collection generally provides two general map implementations, one is HashMap, the other is TreeMap, and the following is a detailed analysis of the implementation of these two types of maps:
HASHMAP: Based on hash table implementation. The key classes you add with HashMap explicitly define hashcode () and Equals () [can override Hashcode () and Equals ()], and to optimize 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): Constructs 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): Constructs an image tree, adds all the mappings in the image tree S, and sorts with the same comparer as the ordered image s

3.HashMap uses Hashcode to quickly find its content, while all elements in the TreeMap are kept in a fixed order,
If you need to get an ordered result you should use TREEMAP (the order of the elements in the HashMap is not fixed).

4. Threading: HashMap Non-thread-safe TREEMAP thread safety
In Java, thread safety generally manifests itself in two ways:
<1 multiple thread accesses to the same Java instance (read and modify) do not interfere with each other, it is mainly embodied in the keyword synchronized. such as ArrayList and Vector,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, and there is no Java keyword support, such as static, transient.

5.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. The two mappings are equal if the two map sizes are equal, the same keys are included, 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 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 provides an access method for the view (subset) of the image, including two endpoints. In addition to sorting is a key that works on a map, processing SortedMap is the same as dealing with SortedSet. The element added to the SortedMap implementation class must implement the comparable interface, otherwise you must give its constructor an implementation of the comparator interface. The TreeMap class is its only implementation.

6. 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.

7. Summary: HashMap is usually faster than treemap (the data structure of the tree and the hash table), it is recommended to use HashMap, when the need to sort the map is used TreeMap

The differences and connections between TreeMap and HashMap in Java:

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.