Worse case time complexity put/get HashMap

前端 未结 5 1980
后悔当初
后悔当初 2021-02-10 22:41

What is the worst case time complexity of an Hashmap when the hashcode of it\'s keys are always equal.

In my understanding: As every key has the same hashcode it will al

相关标签:
5条回答
  • 2021-02-10 23:33

    When inserting, it doesn't matter where in the bucket you put it, so you can just insert it anywhere, thus insertion is O(1).

    Lookup is O(n) because you will have to loop through each object and verify that it is the one you were looking for (as you've stated).

    0 讨论(0)
  • 2021-02-10 23:39

    Yes, in the worst case your hash map will degenerate into a linked list and you will suffer an O(N) penalty for lookups, as well as inserts and deletions, both of which require a lookup operation (thanks to the comments for pointing out the mistake in my earlier answer).

    There are some ways of mitigating the worst-case behavior, such as by using a self-balancing tree instead of a linked list for the bucket overflow - this reduces the worst-case behavior to O(logn) instead of O(n).

    0 讨论(0)
  • 2021-02-10 23:43

    HasMap Complexity

    
              Best.  Avg.   Worst
      Search  O(1)   O(1)   O(n)
      Insert  O(1)   O(1)   O(n)
      Delete  O(1)   O(1)   O(n)
    

    Hope that will help in short

    0 讨论(0)
  • 2021-02-10 23:47

    In Java 8's HashMap implementation:

    Handle Frequent HashMap Collisions with Balanced Trees: In the case of high hash collisions, this will improve worst-case performance from O(n) to O(log n).

    From here.

    0 讨论(0)
  • 2021-02-10 23:48

    in open hashing, you will have a linked list to store objects which have the same hashcode. so: for example, you have a hashed table with size 4. 1) assume you want to store an object with hashcode = 0. the object then will be mapped into index (0 mod 4 = ) 0. 2) then you again want to put another object with hashcode = 8. this object will be mapped into index (8 mod 4 = ) 0, as we remember that the index 0 has already filled with our first object, so we have to put the second next to the first.

    [0]=>linkedList{object1, object2}
    [1]=>null
    [2]=>null
    [3]=>null
    

    3) what are the steps for searching? 1st, you have to hash the key object and assume that it hashcode is 8, so you will be redirected to index (8 mod 4 = ) 0, then because there is more than one object stored in the same index, we have to search one-by-one all stored objects in the list until you find the matched one or until the end of the list. as the example has 2 objects which stored in the same hashtable index 0, and the searched object lies right in the end of the linkedlist, so you need to walk through all the stored objects. that's why it is O(n) as the worst case. worst case occured when all the stored object are in the same index in the hashtable. so they will be stored in a linkedlist in which we (may) need to walk through all of them to find our searched object.

    hope this help,.

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