This question already has an answer here:
这个问题已经有了答案:
- Difference between HashMap, LinkedHashMap and TreeMap 16 answers
- HashMap, LinkedHashMap和TreeMap 16答案之间的区别
I started learning Java. When would I use a HashMap over a TreeMap?
我开始学习Java。什么时候我可以在TreeMap上使用HashMap ?
8 个解决方案
#1
204
TreeMap
is an example of a SortedMap
, which means that the order of the keys can be sorted, and when iterating over the keys, you can expect that they will be in order.
TreeMap是一个排序映射的示例,这意味着可以对键的顺序进行排序,当遍历键时,可以预期它们将是有序的。
HashMap
on the other hand, makes no such guarantee. Therefore, when iterating over the keys of a HashMap
, you can't be sure what order they will be in.
另一方面,HashMap没有这样的保证。因此,当迭代HashMap的键时,您不能确定它们的顺序。
HashMap
will be more efficient in general, so use it whenever you don't care about the order of the keys.
一般来说,HashMap会更有效,所以当您不关心键的顺序时就使用它。
#2
53
HashMap
is implemented by Hash Table while TreeMap
is implemented by Red-Black tree
. The main difference between HashMap
and TreeMap
actually reflect the main difference between a Hash
and a Binary Tree
, that is, when iterating, TreeMap guarantee can the key order which is determined by either element's compareTo() method or a comparator set in the TreeMap's constructor.
HashMap是由哈希表实现的,TreeMap是由红黑树实现的。HashMap和TreeMap之间的主要区别实际上反映了散列和二叉树之间的主要区别,也就是说,当迭代时,TreeMap保证可以由元素的compareTo()方法或TreeMap的构造函数中设置的比较器确定的键顺序。
Take a look at following diagram.
请看下图。
#3
31
To sum up:
总结:
- HashMap: Lookup-array structure, based on hashCode(), equals() implementations, O(1) runtime complexity for inserting and searching, unsorted
- HashMap:查询数组结构,基于hashCode()、equals()实现,O(1)用于插入和搜索的运行时复杂性,未排序
- TreeMap: Tree structure, based on compareTo() implementation, O(log(N)) runtime complexity for inserting and searching, sorted
- 树结构,基于compareTo()实现,O(log(N))运行时复杂度插入和搜索,排序
Taken from: HashMap vs. TreeMap
选自:HashMap vs. TreeMap
#4
21
Use HashMap
most of the times but use TreeMap
when you need the key to be sorted (when you need to iterate the keys).
大多数时候使用HashMap,但是当需要对键进行排序时(当需要迭代键时)使用TreeMap。
#5
14
I'll talk about the HashMap and TreeMap implementation in Java:
我将在Java中讨论HashMap和TreeMap实现:
-
HashMap -- implement basic map interface
HashMap——实现基本的映射接口。
- implemented by an array of buckets, each bucket is a LinkedList of entries
- 由bucket数组实现,每个bucket都是一个条目链表
- running time of basic operations: put(), average O(1), worst case O(n), happens when the table is resized; get(), remove(), average O(1)
- 基本操作的运行时间:put(),平均O(1),最坏情况O(n),表调整大小时发生;get()、删除(),平均O(1)
- not synchronized, to synchronize it:
Map m = Collections.synchronizedMap(new HashMap(...));
- 不同步,同步:Map m =集合。synchronizedMap(新HashMap(…));
- Iteration order of the map is unpredictable.
- 映射的迭代顺序是不可预测的。
-
TreeMap -- implement navigable map interface
TreeMap——实现可导航地图接口
- implemented by a red-black tree
- 由红黑树实现
- running time of basic operations: put(), get(), remove(), worst case O(lgn)
- 基本操作的运行时间:put()、get()、remove()、最坏情况O(lgn)
- not synchronized, to synchronize it:
SortedMap m = Collections.synchronizedSortedMap(new TreeMap(...));
- 不同步,同步:SortedMap m =集合。synchronizedSortedMap(新TreeMap(…));
- provide ordered iteration. higherKey(), lowerKey() can be used to get the successor and predecessor of a given key.
- 提供命令迭代。可以使用higherKey()、lowerKey()获取给定密钥的继承和继承。
To sum, the biggest difference between HashMap and TreeMap is that TreeMap implements NavigableMap<K,V>
, which provide the feature of ordered iteration. Besides, both HashMap and TreeMap are members of Java Collection framework. You can investigate the source code of Java to know more about their implementations.
总的来说,HashMap和TreeMap最大的区别在于TreeMap实现了NavigableMap
#6
7
You almost always use HashMap
, you should only use TreeMap
if you need your keys to be in a specific order.
您几乎总是使用HashMap,如果需要按特定的顺序使用密钥,则应该只使用TreeMap。
#7
7
HashMap
is used for fast lookup, whereas TreeMap
is used for sorted iterations over the map.
HashMap用于快速查找,而TreeMap用于对映射进行排序的迭代。
#8
5
Along with sorted key store one another difference is with TreeMap, developer can give (String.CASE_INSENSITIVE_ORDER) with String keys, so then the comparator ignores case of key while performing comparison of keys on map access. This is not possible to give such option with HashMap - it is always case sensitive comparisons in HashMap.
在排序密钥存储库中,另一个区别是TreeMap,开发人员可以使用字符串键提供(String. case_inve_order),因此,比较器在对map访问的键进行比较时忽略了key的情况。在HashMap中不可能提供这样的选项——在HashMap中总是区分大小写的比较。
#1
204
TreeMap
is an example of a SortedMap
, which means that the order of the keys can be sorted, and when iterating over the keys, you can expect that they will be in order.
TreeMap是一个排序映射的示例,这意味着可以对键的顺序进行排序,当遍历键时,可以预期它们将是有序的。
HashMap
on the other hand, makes no such guarantee. Therefore, when iterating over the keys of a HashMap
, you can't be sure what order they will be in.
另一方面,HashMap没有这样的保证。因此,当迭代HashMap的键时,您不能确定它们的顺序。
HashMap
will be more efficient in general, so use it whenever you don't care about the order of the keys.
一般来说,HashMap会更有效,所以当您不关心键的顺序时就使用它。
#2
53
HashMap
is implemented by Hash Table while TreeMap
is implemented by Red-Black tree
. The main difference between HashMap
and TreeMap
actually reflect the main difference between a Hash
and a Binary Tree
, that is, when iterating, TreeMap guarantee can the key order which is determined by either element's compareTo() method or a comparator set in the TreeMap's constructor.
HashMap是由哈希表实现的,TreeMap是由红黑树实现的。HashMap和TreeMap之间的主要区别实际上反映了散列和二叉树之间的主要区别,也就是说,当迭代时,TreeMap保证可以由元素的compareTo()方法或TreeMap的构造函数中设置的比较器确定的键顺序。
Take a look at following diagram.
请看下图。
#3
31
To sum up:
总结:
- HashMap: Lookup-array structure, based on hashCode(), equals() implementations, O(1) runtime complexity for inserting and searching, unsorted
- HashMap:查询数组结构,基于hashCode()、equals()实现,O(1)用于插入和搜索的运行时复杂性,未排序
- TreeMap: Tree structure, based on compareTo() implementation, O(log(N)) runtime complexity for inserting and searching, sorted
- 树结构,基于compareTo()实现,O(log(N))运行时复杂度插入和搜索,排序
Taken from: HashMap vs. TreeMap
选自:HashMap vs. TreeMap
#4
21
Use HashMap
most of the times but use TreeMap
when you need the key to be sorted (when you need to iterate the keys).
大多数时候使用HashMap,但是当需要对键进行排序时(当需要迭代键时)使用TreeMap。
#5
14
I'll talk about the HashMap and TreeMap implementation in Java:
我将在Java中讨论HashMap和TreeMap实现:
-
HashMap -- implement basic map interface
HashMap——实现基本的映射接口。
- implemented by an array of buckets, each bucket is a LinkedList of entries
- 由bucket数组实现,每个bucket都是一个条目链表
- running time of basic operations: put(), average O(1), worst case O(n), happens when the table is resized; get(), remove(), average O(1)
- 基本操作的运行时间:put(),平均O(1),最坏情况O(n),表调整大小时发生;get()、删除(),平均O(1)
- not synchronized, to synchronize it:
Map m = Collections.synchronizedMap(new HashMap(...));
- 不同步,同步:Map m =集合。synchronizedMap(新HashMap(…));
- Iteration order of the map is unpredictable.
- 映射的迭代顺序是不可预测的。
-
TreeMap -- implement navigable map interface
TreeMap——实现可导航地图接口
- implemented by a red-black tree
- 由红黑树实现
- running time of basic operations: put(), get(), remove(), worst case O(lgn)
- 基本操作的运行时间:put()、get()、remove()、最坏情况O(lgn)
- not synchronized, to synchronize it:
SortedMap m = Collections.synchronizedSortedMap(new TreeMap(...));
- 不同步,同步:SortedMap m =集合。synchronizedSortedMap(新TreeMap(…));
- provide ordered iteration. higherKey(), lowerKey() can be used to get the successor and predecessor of a given key.
- 提供命令迭代。可以使用higherKey()、lowerKey()获取给定密钥的继承和继承。
To sum, the biggest difference between HashMap and TreeMap is that TreeMap implements NavigableMap<K,V>
, which provide the feature of ordered iteration. Besides, both HashMap and TreeMap are members of Java Collection framework. You can investigate the source code of Java to know more about their implementations.
总的来说,HashMap和TreeMap最大的区别在于TreeMap实现了NavigableMap
#6
7
You almost always use HashMap
, you should only use TreeMap
if you need your keys to be in a specific order.
您几乎总是使用HashMap,如果需要按特定的顺序使用密钥,则应该只使用TreeMap。
#7
7
HashMap
is used for fast lookup, whereas TreeMap
is used for sorted iterations over the map.
HashMap用于快速查找,而TreeMap用于对映射进行排序的迭代。
#8
5
Along with sorted key store one another difference is with TreeMap, developer can give (String.CASE_INSENSITIVE_ORDER) with String keys, so then the comparator ignores case of key while performing comparison of keys on map access. This is not possible to give such option with HashMap - it is always case sensitive comparisons in HashMap.
在排序密钥存储库中,另一个区别是TreeMap,开发人员可以使用字符串键提供(String. case_inve_order),因此,比较器在对map访问的键进行比较时忽略了key的情况。在HashMap中不可能提供这样的选项——在HashMap中总是区分大小写的比较。