Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to compare HashMap and TreeMap

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

In this issue, the editor will bring you about how to compare HashMap and TreeMap. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

1.AbstractMap Abstract Class and SortedMap Interface

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

SortedMap interface: it is used to keep the keys in order. The SortedMap interface provides access to the view (subset) of the image, including two endpoints. Except that sorting is the key that acts on the map, SortedMap is treated the same as SortedSet. The element added to the SortedMap implementation class must implement the Comparable interface, or you must provide an implementation of the Comparator interface to its constructor. The TreeMap class is an implementation of it.

two。 Two regular Map implementations (inherited from AbstractMap and SortedMap, respectively)

HashMap: based on hash table implementation. Using the key classes that HashMap requires to add clearly defines hashCode () and equals () [you can override hashCode () and equals ()], and you can tune the initial capacity and load factor to optimize the use of HashMap space.

(1) HashMap (): build an empty hash image

(2) HashMap (Map m): build a hash image and add all mappings of the image m

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

(4) HashMap (int initialCapacity, float loadFactor): construct an empty hash image with specific capacity and loading factors.

TreeMap: based on red-black tree. TreeMap has no tuning option because the tree is always in a balanced state.

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

(2) TreeMap (Map m): build an image tree and add all the elements in the 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 using the same comparator as the ordered image s

3. Two conventional Map performanc

HashMap: suitable for inserting, deleting, and locating elements in Map.

Treemap: suitable for traversing keys in natural or custom order (key).

4. Summary: HashMap is usually a little faster than TreeMap (due to the data structure of trees and hash tables). It is recommended to use HashMap more often and use TreeMap only when sorted Map is needed.

The above is the editor for you to share how to compare HashMap and TreeMap, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report