How will Hashmap key behave if hash code is overridden such that it returns only a constant number?

前端 未结 3 1074
一向
一向 2021-01-19 05:49

I have a small question about Java Hashmap. If I override the hashCode method such that:

@Override
public int hashCode(){
  return          


        
3条回答
  •  醉梦人生
    2021-01-19 06:28

    Hashcode values are used to reduce the search time of an object . The hashcode value does not necessarily be unique for distinct objects. The hashCode() method may in fact, be overridden in such a way that it returns a constant integer for all objects (this would however defeat the purpose of hashCode() method). However, the default implementation of class Object does return a unique integer for every object, as it maps the internal address of the object to an integer and returns the same. But this is not a requirement

提交回复
热议问题