Object in Java has hashCode method, however, it is being used only in associative containers like HashSet or HashMap. Why was it designed like that? Hashable interface having hashCode method looks as much more elegant solution.
Object in Java has hashCode method, however, it is being used only in associative
Share
The major argument seems to me that there is a well-defined default
hashCodethat can be calculated for any Java object, along with an equally well-definedequals. There is simply no good reason to withhold this function from all objects, and of course there are plenty reasons not to withhold it. So it’s a no-brainer in my book.