Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is HashMap containsKey slower than get in Sun JDK? (sun-jdk-1.6.0.17)

Tags:

People also ask

Is containsKey faster than get?

It takes approximately the same amount of time to call get and containsKey, and it's virtually guaranteed that after you call containsKey, you're going to call get anyways, so you may as well cut out the middle man.

Why is hashmap slow?

First it slows down due to intense GC'ing and then it breaks with OutOfMemoryError . See my reply on how to cut down the memory use by an estimated factor of 5-10, by using GNU Trove primitive collections. Roughly estimated, a HashMap<Integer, List<>> needs at least 3*16 bytes per entry.

What is the time complexity of map containsKey?

Time Complexity: Time Complexity of HashMap. containsKey() is O(1) in Average Case, and O(n) in worst case.

Which is faster than hashmap?

I have used an array instead of a hashmap and there is a lot of difference between the time complexities of both methods and I am not able to understand why as according to the post on stackoverflow array is faster than hashmap.


Why is calling containsKey on a HashMap slower then get?

Test: http://ideone.com/QsWXF (>15% difference, run on sun-jdk-1.6.0.17)