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.
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.
Time Complexity: Time Complexity of HashMap. containsKey() is O(1) in Average Case, and O(n) in worst case.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With