Java中的HashMap和Hashtable有什么区别? [英] What are the differences between a HashMap and a Hashtable in Java?
问题描述
HashMap
和
What are the differences between a HashMap
and a Hashtable
in Java?
对于非线程应用程序,哪种效率更高?
Which is more efficient for non-threaded applications?
推荐答案
-
Hashtable
是已同步,而HashMap
不是.这使HashMap
更适合非线程应用程序,因为未同步对象通常比同步对象执行得更好.
Hashtable
is synchronized, whereasHashMap
is not. This makesHashMap
better for non-threaded applications, as unsynchronized Objects typically perform better than synchronized ones.
Hashtable
不允许使用null
键或值. HashMap
允许一个null
键和任意数量的null
值.
Hashtable
does not allow null
keys or values. HashMap
allows one null
key and any number of null
values.
HashMap的子类之一是 HashMap
换成LinkedHashMap
.如果您使用的是Hashtable
,则不会那么容易.
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 the HashMap
for a LinkedHashMap
. This wouldn't be as easy if you were using Hashtable
.
由于同步对您来说不是问题,因此建议您使用HashMap
.如果同步成为问题,您还可以查看 ConcurrentHashMap
.
Since synchronization is not an issue for you, I'd recommend HashMap
. If synchronization becomes an issue, you may also look at ConcurrentHashMap
.
这篇关于Java中的HashMap和Hashtable有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!