Java中的HashMap和Hashtable有什么区别? [英] What are the differences between a HashMap and a Hashtable in Java?

查看:138
本文介绍了Java中的HashMap和Hashtable有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HashMap

What are the differences between a HashMap and a Hashtable in Java?

对于非线程应用程序,哪种效率更高?

Which is more efficient for non-threaded applications?

推荐答案

Hashtable :

  1. Hashtable已同步,而HashMap不是.这使HashMap更适合非线程应用程序,因为未同步对象通常比同步对象执行得更好.

  1. Hashtable is synchronized, whereas HashMap is not. This makes HashMap 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆