`
qqdwll
  • 浏览: 136699 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

TreeMap vs HashMap

阅读更多
Both TreeMap & HashMap are two different implementation of the Map interface. Even though this post is titled “TreeMap vs HashMap” I would like to say how they are connected and how much similar they are.

Both TreeMap & HashMap are not synchronized. To make it synchronized we have to explicitly call Collections.synchronizedMap( mapName ) . Both supports “fail-fast” iterators. Both of them doesn’t support duplicate keys.

HashMap

HashMap allows null as both keys and values. HashMap is useful when we need to access the map without cosidering how they are added to the map (means, unordered lookup of values using their keys). HashMap is synchronized while it is being looked up. HashMap doesn’t allow duplicated entries.

The performance of HashMap is based on two optional parameter which we can specify during the creation of the HashMap. Initial capacity & load factor. Initial capacity is the bucket size assigned to a HashMap during it’s creation. Load factor decides when the HashMap needs to be expanded. If the load factor is 0.75, the size will be increased when the current size of the map crosses 75% of its capacity.

TreeMap

The basic difference between HashMap & TreeMap is that, in a TreeMap the elements are stored in a tree. TreeMap allows us to retrieve the elements in some sorted order defined by the user. So we can say that TreeMap is slower than HashMap. This is the only implementation based on SortedMap interface.

TreeMap allows us to specify an optional Comparator object during its creation. The keys should be compatible with the comparator specified. This comparator decides the order by which the keys need to be sorted.

public interface Comparator  

2 {  

3     public int compare (Object object1, Object object2);  

4     public boolean equals (Object object);  

5 } 

If we are not specifying the comparator, TreeMap will try to sort the keys in the natural order. Means will consider them as instances of Comparable interface.

public interface Comparable  

2 {  

3     public int compareTo (Object objectToCompare);  

4 } 

Classes like Integer, String, Double etc implements Comparable interface. So if we are to use an object of a custom class as the key, ensure that it’ s class implements the Comparable interface.

public class MyCustomKey implements Comparable  

02 {  

03     private int value;  

04     public MyCustomKey(int value)  

05     {  

06        this.value = value;  

07     }             

08    

09     public int compareTo (MyCustomKey key)  

10     {  

11        int comparison = 0;             

12    

13        // Note:  

14        // Return -1 if this.value < key.value  

15        // Return  0 if this.value = key.value  

16        // Return  1 if this.value > key.value             

17    

18        return (comparison);  

19     }  

20 } 


A common mistake that everyone does is not to override the hashcode(). If we are failing to do so, map.get(new MyCustomKey(<value>)); may not give you what you were expecting. So it is always adviced to override the hashCode() if objects of that class is being used as a key.

public class MyCustomKey implements Comparable  

02 {  

03     private int value;  

04     public MyCustomKey(int value)  

05     {}  

06     public int compareTo (MyCustomKey key)  

07     {}          

08    

09     public int hashCode()  

10     {  

11         // Note:  

12         // If two objects are equal then their corresponding hashCode ()  

13         // should return the same value too.  

14         return (this.value * 199);  

15     }  

16 } 



TreeMap 的底层算法是 自平衡二叉寻找树。 自平衡二叉寻找树相对于主要的竞争者Hash 算法有很多的优缺点。 一个优点就是可以快速的列出排序的Key。

下面给出自平衡二叉寻找树插入自转的图。 删除比较简单。


  • 大小: 39.7 KB
分享到:
评论

相关推荐

    Java里多个Map的性能比较(TreeMap、HashMap、ConcurrentSkipListMap)

    在Java编程中,Map接口是用于存储键值对的数据结构,而Java提供了多种Map的实现,包括TreeMap、HashMap和ConcurrentSkipListMap。本文主要比较了这三种Map的性能,尤其是在插入和查找操作上的效率。 1. **TreeMap**...

    Java中HashMap和TreeMap的区别深入理解

    HashMap和TreeMap是Java中两种常用的Map实现,它们各自具有不同的特性和使用场景。 HashMap是基于哈希表实现的,其核心思想是通过键对象的hashCode()方法来快速定位到对应的桶(bucket),从而提高查找效率。...

    treemap treeset hashset hashmap 简要介绍

    在Java编程语言中,集合框架提供了多种数据结构来存储和操作数据,其中`TreeMap`、`TreeSet`、`HashSet`以及`HashMap`是最常用的数据结构之一。这些集合类各自有着独特的特性和应用场景,下面将对它们进行详细介绍。...

    java中HashMap,LinkedHashMap,TreeMap,HashTable的区别

    ### Java中HashMap, LinkedHashMap, TreeMap,HashTable的区别 在Java编程语言中,`Map`接口是集合框架中的一个重要组成部分,用于存储键值对。本文将详细分析四种常用的`Map`实现类:`HashMap`, `LinkedHashMap`, ...

    HashMap,HashTable,LinkedHashMap,TreeMap的区别

    HashMap, HashTable, LinkedHashMap, TreeMap 的区别 在 Java 中,Map 是一个非常重要的集合类,用于存储键值对。其中,HashMap, HashTable, LinkedHashMap, TreeMap 是四种常用的 Map 实现类,每种类都有其特点和...

    HashMap vs TreeMap vs Hashtable vs LinkedHashMap

    HashMap是最常用的Map实现,它提供了快速的查找,但不保证元素的顺序,且允许null键和值。TreeMap则按Key的自然排序或自定义排序存储元素,适合需要排序的场景。HashTable是线程安全的,但效率相对较低,不推荐在单...

    Map,HashMap,TreeMap的使用

    Java 中的 Map、HashMap、TreeMap 使用详解 Map 是 Java 集合框架中的一个接口,用于存储键值对,根据键可以获取值。Map 中的键不允许重复,但值可以重复。在 Java 中,HashMap、LinkedHashMap、TreeMap 都实现了 ...

    java HashMap,TreeMap与LinkedHashMap的详解

    在Java编程语言中,`HashMap`、`TreeMap`和`LinkedHashMap`都是`java.util.Map`接口的实现,它们提供了不同的数据存储和访问策略。本文将深入探讨这三种数据结构的特点、工作原理以及适用场景。 1. **HashMap** `...

    第9讲 对比Hashtable、HashMap、TreeMap有什么不同?1

    【HashMap、Hashtable、TreeMap详解】 HashMap、Hashtable和TreeMap都是Java中实现Map接口的类,它们用于存储键值对数据,但各自具有不同的特点和使用场景。 HashMap是最常用的Map实现,它通过哈希表(散列表)...

    在Java中如何决定使用 HashMap 还是 TreeMap

    Java 中的 HashMap 和 TreeMap 选择指南 在 Java 中,HashMap 和 TreeMap 都是常用的 Map 实现类,但是在实际开发中,选择哪种 Map 实现类往往让人感到迷茫。下面我们将详细介绍 HashMap 和 TreeMap 的特点、优缺点...

    尚硅谷——Java TreeMap源码解析

    TreeMap与HashMap不同的是,TreeMap的元素是根据键的自然排序或通过Comparator提供的排序来维护键的有序性。TreeMap提供了对排序映射操作的完全控制,因此它比HashMap更适合在需要排序和按键顺序访问元素的场景中...

    Treemap-4.1.2

    8. **特性和性能**:与HashMap相比,TreeMap的插入和查找速度较慢,但由于其有序性,对于需要保持数据排序的应用来说,TreeMap是一个更好的选择。在内存使用方面,由于需要存储额外的排序信息,TreeMap通常比HashMap...

    HashMap排序

    - 使用`TreeMap`:创建一个`TreeMap`对象并传入`ByValueComparator`作为构造函数参数,然后将`HashMap`的所有键值对放入`TreeMap`中。 - 使用`Collections.sort()`:创建一个包含所有键的`ArrayList`,然后调用`...

    HASHMAP排序功能描述

    - 如果对HashMap进行大量的排序操作,考虑使用TreeMap,它默认按照key的自然顺序排序,也可以自定义Comparator。 **5. 结论** HashMap排序并不是HashMap本身的功能,而是通过其他手段实现的。根据实际需求,可以...

    JAVA中HashMap的用法.docx

    在示例代码中,我们创建了三个Map实例:HashMap、Hashtable和TreeMap。HashMap展示了无序的特性,而TreeMap则按顺序打印键值对。Hashtable是HashMap的线程安全版本,但在Java 5之后,推荐使用并发集合如...

    Java-HashMap.rar_hashmap_java hashmap

    如果需要有序的映射关系,可以使用`TreeMap`。 3. **容量与负载因子**:`HashMap`有一个初始容量(默认为16)和负载因子(默认为0.75)。当元素数量达到容量与负载因子的乘积时,`HashMap`会自动进行扩容,将容量翻...

    HashMap底层原理.pdf

    HashMap是Java中非常常见的一种数据结构,主要用于存储键值对,其核心原理是通过哈希算法将键映射到数组中的位置来实现快速访问。本文将详细介绍HashMap的底层原理,包括其内部实现结构、关键字段的作用、以及JDK ...

    Hashmap 通过对VALUE排序 源代码

    HashMap是Java编程语言中最常用的集合类之一,它提供了一...如果需要在HashMap中直接排序,可能需要结合其他数据结构如TreeMap或LinkedHashMap。如果想深入了解这些源代码,建议直接访问给出的博客链接以获取详细信息。

    HashMap集合排序

    总结一下,`HashMap` 和 `TreeMap` 都是 Java 中的集合类,但它们有不同的特性。`HashMap` 提供快速存取但无序,而 `TreeMap` 可以自动排序。在这个例子中,我们利用 `TreeMap` 的排序功能,结合自定义的 `Car` 类,...

Global site tag (gtag.js) - Google Analytics