First I want to say what I know about javadoc java.lang.Object.hashCode()
, and therefore there is no need to mention it again.
I ask: Why didn’t I java.lang.Object.hashCode()
move to a separate interface with the name (possibly) Hashable
? How is `java.lang.Comparator '?
For me it is hashCode()
used only in hash-dependent data structures, such as HashMap
or HashTable
, which are not used in every application b) are often used with very few types of keys, such as String
or Integer
, and not with InputStream
(or something like this).
I know that I do not need to implement hashCode()
for each of my classes, however, does not add a method to the class due to performance to some extent? Especially for java.lang.Object
- the superclass of every class in Java.
Even if special optimization is performed in the JVM, so that performance loss can be ignored, I still find it unwise to provide everyone with Object
behavior that is not often implemented at all. In accordance with the principle of separation of circuits :
No client should depend on methods that it does not use.
I have done several searches on the Internet, and the only related page I could find is this .
() , , , , "hashCode()
Object
HashMap
", .
, , - HashMap
:
hashCode()
java.lang.Object
.Hashable
, hashCode()
, java.lang.Object.hashCode()
.HashProvider
T
, provideHashCode(T t)
, - . ( Comparator<T>
).HashProvider<Object>
DefaultHashProvider
, - Object
, Object.hashCode()
. ( Java 8, Object.hashCode() , , DefaultHashProvider.provideHashCode()
Object
)HashMap
HashTable
, :provideHashCode()
, HashProvider
.hashCode()
, Hashable
.DefaultHashProvider
.
, , Comparable
, Comparator
TreeMap
.
:
, Java , , ? , ? : ?
- , , , Java - 1,5. , ,
Comparator
, Comparable
TreeMap
1.2, , , HashMap
? hashCode()
- JVM, Object
-. DefaultHashProvider.provideHashCode()
( ) -hashables.