Key existence check in HashMap

后端 未结 10 1986
走了就别回头了
走了就别回头了 2020-11-27 09:03

Is checking for key existence in HashMap always necessary?

I have a HashMap with say a 1000 entries and I am looking at improving the efficiency. If the HashMap is b

10条回答
  •  时光说笑
    2020-11-27 09:51

    1. If key class is your's make sure the hashCode() and equals() methods implemented.
    2. Basically the access to HashMap should be O(1) but with wrong hashCode method implementation it's become O(n), because value with same hash key will stored as Linked list.

提交回复
热议问题