java Linkedhashmap、hashmap、map、hashtable 的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6382817/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
difference between linkedhashmap, hashmap, map, hashtable
提问by Amm Sokun
I am preparing for software interviews and i am stuck with a question for days now.
我正在准备软件面试,现在我被一个问题困扰了好几天。
I have not been able to figure out the difference between linkedhashmap, map, hashtable, hashmap present in the Java Collection API.
我一直无法弄清楚 Java Collection API 中存在的linkedhashmap、map、hashtable、hashmap 之间的区别。
Do all of these have the same get and put complexities? I know that map is the interface class and hashmap, hashtable, linkedhashmap implement this interface. So does that mean that the inner implementation of these 3 classes is the same? How are they implemented in the collections api?
所有这些都具有相同的 get 和 put 复杂性吗?我知道map是接口类,hashmap、hashtable、linkedhashmap实现了这个接口。那么这是否意味着这 3 个类的内部实现是相同的呢?它们是如何在集合 api 中实现的?
Thanks in Advance!!!
提前致谢!!!
回答by Waldheinz
I doubt the differences can be explained significantly better than what's already written in the JavaDocs for these classes:
我怀疑与 JavaDocs 中为这些类编写的内容相比,可以更好地解释这些差异:
- Mapis the basic interface common to all these classes
- a Hashtableis one implementation of that interface, for the "old" days when it was thought that having everything synchronized is a good idea (ref. Vector). It offers "kind of" thread-safety, if you know what you are doing. If you are serious about a map which can be used from multiple threads you should absolutely check out the
ConcurrentHashMap
andConcurrentSkipListMap
. - a HashMapis almost the same as a Hashtable, but with the synchronization removed. It's the preferred general-purpose Map implementation.
- a LinkedHashMapadditionally maintains a linked list of it's entries, which allows to maintain an ordering or use it as a LRU cache easily, just read the JavaDoc.
- Map是所有这些类通用的基本接口
- 一个Hashtable的是一个实现该接口的,因为当它被认为拥有一切同步是一个好主意(参见“老字号”天矢量)。如果您知道自己在做什么,它会提供“某种”线程安全性。如果您认真对待可以从多个线程使用的地图,您绝对应该检查
ConcurrentHashMap
和ConcurrentSkipListMap
。 - 一个HashMap中是几乎相同哈希表,但与同步去除。它是首选的通用 Map 实现。
- 一个LinkedHashMap的额外维护它的条目,这使得维持排序或轻松地使用它作为一个LRU缓存,刚读的JavaDoc的链表。
All of the aforementioned Map
implementations have their basic get/put operations in (amortized) O(1)time complexity. There are minor differences in the handling of null
values, it's inevitable to check the JavaDoc for details.
所有上述Map
实现都有其基本的 get/put 操作(分摊)O(1)时间复杂度。null
值的处理存在细微差别,详细信息请查看 JavaDoc。
To get an idea of how these classes are implemeted, have a look at their inheritance tree:
要了解这些类是如何实现的,请查看它们的继承树:
Map
(just the interface)Dictionary
(obsoleted abstract class)Hashtable
(the "old" map implementation lives on it's own)
AbstractMap
(the basic functionality of the "new" map implementations)HashMap
(the first concrete map implementation for general purpose use)LinkedHashMap
(extendsHashMap
by mainaining the linked list)
Map
(只是界面)Dictionary
(已过时的抽象类)Hashtable
(“旧”地图实现独立存在)
AbstractMap
(“新”地图实现的基本功能)HashMap
(第一个用于通用用途的具体地图实现)LinkedHashMap
(HashMap
通过维护链表进行扩展)
回答by stevevls
They all honor the same contract, but there are some differences in the implementation:
它们都遵守相同的合同,但在实现上存在一些差异:
- LinkedHashMap : keys are maintained in insertion order
- HashTable : all operations are synchronized, no ordering guarantees
- HashMap : no ordering guarantees, best performance
- LinkedHashMap :键按插入顺序维护
- HashTable :所有操作都是同步的,没有排序保证
- HashMap :无排序保证,最佳性能
Generally, the best practice is to use Map
as the type for variables, and then you instantiate an implementing type based on the needs of your code. HashMap
is generally preferred unless you need some ordering guarantees, in which case LinkedHashMap
or TreeMap
are good choices.
通常,最佳实践是Map
用作变量的类型,然后根据代码的需要实例化一个实现类型。 HashMap
通常首选,除非您需要一些订购保证,在这种情况下LinkedHashMap
或TreeMap
是不错的选择。
回答by jigar
All classes implement the Map interface and offer mostly the same functionality. The most important difference is the order in which iteration through the entries will happen:
所有类都实现了 Map 接口并提供了大致相同的功能。最重要的区别是通过条目进行迭代的顺序:
HashMap makes absolutely no guarantees about the iteration order. It can (and will) even change completely when new elements are added. TreeMap will iterate according to the "natural ordering" of the keys according to their compareTo() method (or an externally supplied Comparator). Additionally, it implements the SortedMap interface, which contains methods that depend on this sort order. LinkedHashMap will iterate in the order in which the entries were put into the map "Hashtable" is the generic name for hash-based maps. In the context of the Java API, Hashtable is an obsolete class from the days of Java 1.1 before the collections framework existed. It should not be used anymore, because its API is cluttered with obsolete methods that duplicate functionality, and its methods are synchronized (which can decrease performance and is generally useless). Use ConcurrrentHashMap instead of Hashtable.
HashMap 绝对不保证迭代顺序。当添加新元素时,它甚至可以(并且将会)完全改变。TreeMap 将根据它们的 compareTo() 方法(或外部提供的 Comparator)根据键的“自然顺序”进行迭代。此外,它还实现了 SortedMap 接口,该接口包含依赖于该排序顺序的方法。LinkedHashMap 将按照条目放入映射的顺序进行迭代“Hashtable”是基于散列的映射的通用名称。在 Java API 的上下文中,Hashtable 在集合框架存在之前的 Java 1.1 时代是一个过时的类。不应再使用它,因为它的 API 中充斥着重复功能的过时方法,并且它的方法是同步的(这会降低性能并且通常是无用的)。使用 ConcurrrentHashMap 而不是 Hashtable。