linkedhashmap,hashmap,map,hashtable的区别 [英] difference between linkedhashmap, hashmap, map, hashtable

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

问题描述



我已经无法弄清楚linkedhashmap,map,map之间的区别, hashtable,hashmap存在于Java Collection API中。



所有这些都有相同的get和put复杂性吗?我知道map是接口类
和hashmap,hashtable,linkedhashmap实现这个接口。这是否意味着这3个类的内部实现是一样的?如何在集合api中实现它们?



感谢在前进!!!

解决方案

我怀疑这些差异可以被解释得明显好于这些类的JavaDocs中写的:




  • 地图是所有这些类通用的基本界面

  • Hashtable 是该界面的一种实现方式,适用于老的日子,当它被认为有一切同步是一个好主意(参考矢量)。它提供了种类线程安全,如果你知道你在做什么。如果你对一个地图可以从多个线程使用,你应该绝对检查 ConcurrentHashMap ConcurrentSkipListMap

  • a HashMap 几乎与Hashtable相同,但删除了同步。这是首选的通用地图实现。

  • a LinkedHashMap 还保留了它的条目的链接列表,这允许维护一个排序或者容易地将其用作LRU缓存,只需读取JavaDoc。



所有上述 Map 实现的基本get / put操作都在(摊销) O(1)时间复杂性。在处理 null 值时存在细微差异,因此无法检查JavaDoc的详细信息。



一个关于如何实现这些类的想法,看看它们的继承树:




  • 映射(只是界面)

    • 字典(废弃的抽象类)

      • Hashtable (旧的地图实现仍然属于自己)


    • AbstractMap (新地图实现的基本功能)

      • HashMap (一般用途的第一个具体地图实现)

        • LinkedHashMap code> HashMap 通过维护链接列表)





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.

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?

Thanks in Advance!!!

解决方案

I doubt the differences can be explained significantly better than what's already written in the JavaDocs for these classes:

  • Map is the basic interface common to all these classes
  • a Hashtable is 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 and ConcurrentSkipListMap.
  • a HashMap is almost the same as a Hashtable, but with the synchronization removed. It's the preferred general-purpose Map implementation.
  • a LinkedHashMap additionally 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.

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.

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 (extends HashMap by mainaining the linked list)

这篇关于linkedhashmap,hashmap,map,hashtable的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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