HashMap和Hashtable的区别? [英] Differences between HashMap and Hashtable?
问题描述
有什么区别 HashMap
和 Hashtable
在Java中?
有效的非线程应用程序?
-
哈希表
是同步,而HashMap
不是。这使得非线程应用程序的HashMap
更好,因为非同步对象通常比同步对象执行更好。 -
其中一个HashMap的子类是
LinkedHashMap
,所以如果您想要可预测的迭代顺序(默认为插入顺序) ,您可以轻松地为LinkedHashMap
换出HashMap
。如果你使用Hashtable
,这不会那么容易。
Hashtable
不允许 null
键或值。 HashMap
允许一个 null
键和任意数量的 null
值。
既然同步对你来说不是问题,我推荐 HashMap
。如果同步成为问题,您也可以查看 ConcurrentHashMap
。
What are the differences between a HashMap
and a Hashtable
in Java?
Which is more efficient for non-threaded applications?
There are several differences between HashMap
and Hashtable
in Java:
Hashtable
is synchronized, whereasHashMap
is not. This makesHashMap
better for non-threaded applications, as unsynchronized Objects typically perform better than synchronized ones.Hashtable
does not allownull
keys or values.HashMap
allows onenull
key and any number ofnull
values.One of HashMap's subclasses is
LinkedHashMap
, so in the event that you'd want predictable iteration order (which is insertion order by default), you could easily swap out theHashMap
for aLinkedHashMap
. This wouldn't be as easy if you were usingHashtable
.
Since synchronization is not an issue for you, I'd recommend HashMap
. If synchronization becomes an issue, you may also look at ConcurrentHashMap
.
这篇关于HashMap和Hashtable的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!