5

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 always go to the same bucket and loop through it to check for equals method so for both get and put the time complexity should be O(n), Am I right?

I was looking at this HashMap get/put complexity but it doesn't answer my question.

Also here Wiki Hash Table they state the worse case time complexity for insert is O(1) and for get O(n) why is it so?

Community
  • 1
  • 1
Vishal
  • 17,727
  • 17
  • 72
  • 91

5 Answers5

14

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).

sk.
  • 6,066
  • 4
  • 36
  • 45
  • So put doesn't need to loop through the bucket to check equals and if it returns true then replace otherwise just add? leading complexity to O(n)? – Vishal Nov 17 '11 at 05:36
  • This statement is wrong "O(1) insert can be achieved if new object is added to head of Bucket". The reason being you have to iterate through linked list to determine if that key is already present in Map or not. Say i am inserting 1,3,4,3 .Now when i insert element at index 3 in my map, it would fall in same bucket as others, and now my map already contains 3(element at index 1) so this 3 should not be inserted anymore.And for checking that 3 is already there i need to iterate through list. – Algorithmist Mar 31 '13 at 03:01
  • For good hash functions the worst case is always `logn`, see my answer at http://stackoverflow.com/questions/4553624/hashmap-get-put-complexity/23954819#23954819 – Thomas Ahle May 30 '14 at 12:37
2

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.

Abdul
  • 997
  • 2
  • 19
  • 37
2

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,.

simaremare
  • 391
  • 1
  • 9
0

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

Petr Aleksandrov
  • 1,100
  • 7
  • 19
saurabhshcs
  • 107
  • 1
  • 3
-6

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).

Jon Newmuis
  • 23,149
  • 2
  • 45
  • 55
  • Why this O(n) ? We are getting bucket index of HashMap before looking into an item, so we do not need to look into entire bucket, instead the specified bucket and its elements (Entry Object). – Tony May 16 '13 at 04:32
  • If everything has a hashcode of 1, then the "specified bucket and its elements" is everything in the map. – Jon Newmuis May 16 '13 at 06:15
  • you need to do a lookup to do an insert. ergo O(n) – Nova Ardent Dec 06 '18 at 14:06