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

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

问题描述

HashMap 和一个 Hashtable 在 Java 中?

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

哪个对于非线程应用程序更有效?

Which is more efficient for non-threaded applications?

推荐答案

HashMapJava 中的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 的子类之一是 LinkedHashMap,所以如果您想要可预测的迭代顺序(默认情况下是插入顺序),您可以轻松地将 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天全站免登陆