HashMap和Hashtable的区别? [英] Differences between HashMap and Hashtable?

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

问题描述

有什么区别 HashMap Hashtable 在Java中?



有效的非线程应用程序?

解决方案

HashMap Hashtable 在Java :


  1. 哈希表同步,而 HashMap 不是。这使得非线程应用程序的 HashMap 更好,因为非同步对象通常比同步对象执行更好。

  2. Hashtable 不允许 null 键或值。 HashMap 允许一个 null 键和任意数量的 null 值。

  3. 其中一个HashMap的子类是 LinkedHashMap ,所以如果您想要可预测的迭代顺序(默认为插入顺序) ,您可以轻松地为 LinkedHashMap 换出 HashMap 。如果你使用 Hashtable ,这不会那么容易。


既然同步对你来说不是问题,我推荐 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:

  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.

  2. Hashtable does not allow null keys or values. HashMap allows one null key and any number of null values.

  3. 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.

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屋!

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