Name: skT88420 Date: 09/07/99
The Java 2 SDK includes the classes HashMap, HashSet and Hashtable
in the java.util package. These classes are all implemented based
on the usual meaning of Object.equals and Object.hashCode.
However, suppose I want to create a HashMap (or HashSet, or Hashtable) where
the usual rules don't apply. For example, I want to create a
HashMap where two keys are identical if and only if they are the
exact same object, regardless of the result of Object.equals. To
accomplish this, I must re-implement nearly all of HashMap just
to make this one little change. My life would be much simpler if
there were a couple simple functions which I could override to
re-define HashMap's concept of a hashCode.
If HashMap, HashSet and Hashtable used the following methods,
instead of calling hashCode and equals directly on the key, then
it would be possible to override them if necessary to customize
their behavior.
protected int hashCode(Object o) {
return (o != null) ? o.hashCode() : 0;
}
protected boolean equals(Object a, Object b) {
return (a == b) || ((a != null) && a.equals(b));
}
For example, I could implement the behavior I described earlier
by simply extending the HashMap class and overriding the above
methods as...
protected int hashCode(Object o) {
return System.identityHashCode(o);
}
protected boolean equals(Object a, Object b) {
return (a == b);
}
(Review ID: 94977)
======================================================================
We could create a Hasher interface for the suggested methods.
public interface Hasher
{
public int hashCode(Object hashMe);
public boolean equals(Object a, Object b);
}
These methods are helpful for creating a case-insensitive HashMap.
###@###.###
======================================================================
The Java 2 SDK includes the classes HashMap, HashSet and Hashtable
in the java.util package. These classes are all implemented based
on the usual meaning of Object.equals and Object.hashCode.
However, suppose I want to create a HashMap (or HashSet, or Hashtable) where
the usual rules don't apply. For example, I want to create a
HashMap where two keys are identical if and only if they are the
exact same object, regardless of the result of Object.equals. To
accomplish this, I must re-implement nearly all of HashMap just
to make this one little change. My life would be much simpler if
there were a couple simple functions which I could override to
re-define HashMap's concept of a hashCode.
If HashMap, HashSet and Hashtable used the following methods,
instead of calling hashCode and equals directly on the key, then
it would be possible to override them if necessary to customize
their behavior.
protected int hashCode(Object o) {
return (o != null) ? o.hashCode() : 0;
}
protected boolean equals(Object a, Object b) {
return (a == b) || ((a != null) && a.equals(b));
}
For example, I could implement the behavior I described earlier
by simply extending the HashMap class and overriding the above
methods as...
protected int hashCode(Object o) {
return System.identityHashCode(o);
}
protected boolean equals(Object a, Object b) {
return (a == b);
}
(Review ID: 94977)
======================================================================
We could create a Hasher interface for the suggested methods.
public interface Hasher
{
public int hashCode(Object hashMe);
public boolean equals(Object a, Object b);
}
These methods are helpful for creating a case-insensitive HashMap.
###@###.###
======================================================================
- duplicates
-
JDK-6321875 (coll) no equality-function parameter for sets & maps...
- Closed
- relates to
-
JDK-5045681 (coll) Collection implementations should support customized comparison logic
- Open
-
JDK-4771660 (coll) Comparator, Comparable, Identity, and Equivalence
- Open
-
JDK-6435963 (coll) Hasher interface to be used by a HashMap/HashSet
- Closed
-
JDK-6454574 (coll) Support object comparison by identity
- Open
-
JDK-6355410 Impossible for subclasses to preserve the equals contract
- Closed
(1 relates to)