How is the implementation of LinkedHashMap different from HashMap?

前端 未结 8 1254
故里飘歌
故里飘歌 2020-12-28 12:07

If LinkedHashMap\'s time complexity is same as HashMap\'s complexity why do we need HashMap? What are all the extra overhead LinkedHashMap has when compared to HashMap in Ja

相关标签:
8条回答
  • 2020-12-28 12:34

    LinkedHashMap is a useful data structure when you need to know the insertion order of keys to the Map. One suitable use case is for the implementation of an LRU cache. Due to order maintenance of the LinkedHashMap, the data structure needs additional memory compared to HashMap. In case insertion order is not a requirement, you should always go for the HashMap.

    0 讨论(0)
  • 2020-12-28 12:38

    If LinkedHashMap's time complexity is same as HashMap's complexity why do we need HashMap?

    You should not confuse complexity with performance. Two algorithms can have the same complexity, yet one can consistently perform better than the other.

    Remember that f(N) is O(N) means that:

    limit(f(N), N -> infinity) <= C*N  
    

    where C is a constant. The complexity says nothing about how small or large the C values are. For two different algorithms, the constant C will most likely be different.

    (And remember that big-O complexity is about the behavior / performance as N gets very large. It tells you nothing about the behavior / performance for smaller N values.)


    Having said that:

    • The difference in performance between HashMap and LinkedHashMap operations in equivalent use-cases is relatively small.

    • A LinkedHashMap uses more memory. For example, the Java 11 implementation has two additional reference fields in each map entry to represent the before/after list. On a 64 bit platform without compressed OOPs the extra overhead is 16 bytes per entry.

    • Relatively small differences in performance and/or memory usage can actually matter a lot to people with performance or memory critical applications1.

    1 - ... and also to people who obsess about these things unnecessarily.

    0 讨论(0)
  • 2020-12-28 12:38
    • LinkedHashMap additionally maintains a doubly-linked list running through all of its entries, that will provide a reproducable order. This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-order).
    • HashMap doesn't have these extra costs (runtime,space) and should prefered over LinkedHashMap when you don't care about insertion order.
    0 讨论(0)
  • 2020-12-28 12:40

    LinkedHashMap will take more memory. Each entry in a normal HashMap just has the key and the value. Each LinkedHashMap entry has those references and references to the next and previous entries. There's also a little bit more housekeeping to do, although that's usually irrelevant.

    0 讨论(0)
  • 2020-12-28 12:53

    LinkedHashMap inherits HashMap, that means it uses existing implementation of HashMap to store key and values in a Node (Entry Object). Other than this it stores a separate doubly linked list implementation to maintain the insertion order in which keys have been entered.

    It looks like this :

    header node <---> node 1 <---> node 2 <---> node 3 <----> node 4 <---> header node.

    So extra overload is maintaining insertion and deletion in this doubly linked list. Benefit is : Iteration order is guaranteed to be insertion order, which is not in HashMap.

    0 讨论(0)
  • 2020-12-28 12:57

    There is another major difference between HashMap and LinkedHashMap :Iteration is more efficient in case of LinkedHashMap.

    As Elements in LinkedHashMap are connected with each other so iteration requires time proportional to the size of the map, regardless of its capacity. But in case of HashMap; as there is no fixed order, so iteration over it requires time proportional to its capacity.

    I have put more details on my blog.

    0 讨论(0)
提交回复
热议问题