public class HashCode
extends java.lang.Object
The idea here is that objects that define "equals" to mean that a number of member variables are equal needs a hash code that is computed from those same member variables.
Example:
class MyClass {
...
private int membVar1;
private OtherClass membVar2;
private float membVar3;
...
// MyClass objects are equal iff they have the same content.
public boolean equals(Object obj) {
if (obj == this) {
return true;
} else if (obj == null || obj.getClass() != getClass()) {
return false;
}
MyClass cObj = (MyClass)obj;
return (membVar1 == cObj.membVar1)
&& HashCode.equalObjects(membVar2, cObj.membVar2)
&& (membVar3 == bVar3);
}
// The hash code needs to align with the
// definition of equals.
public int hashCode() {
HashCode h = new HashCode();
h.addValue(membVar1);
h.addValue(membVar2);
h.addValue(membVar2);
return h.hashCode();
}
...
}
| Constructor and Description |
|---|
HashCode()
Create a new HashCode object
|
| Modifier and Type | Method and Description |
|---|---|
int |
addValue(boolean b)
Augment the current computed hash code with the
value b.
|
int |
addValue(boolean[] array)
Augment the current computed hash code with the
value array.
|
int |
addValue(byte i)
Augment the current computed hash code with the
value i.
|
int |
addValue(byte[] array)
Augment the current computed hash code with the
value i.
|
int |
addValue(char i)
Augment the current computed hash code with the
value i.
|
int |
addValue(char[] array)
Augment the current computed hash code with the
value array.
|
int |
addValue(double f)
Augment the current computed hash code with the
value f.
|
int |
addValue(double[] array)
Augment the current computed hash code with the
value array.
|
int |
addValue(float f)
Augment the current computed hash code with the
value f.
|
int |
addValue(float[] array)
Augment the current computed hash code with the
value array.
|
int |
addValue(int i)
Augment the current computed hash code with the
value i.
|
int |
addValue(int[] array)
Augment the current computed hash code with the
value array.
|
int |
addValue(java.lang.Object obj)
Augment the current computed hash code with the
value obj.
|
int |
addValue(java.lang.Object[] array)
Augment the current computed hash code with the
value array.
|
int |
addValue(short i)
Augment the current computed hash code with the
value i.
|
int |
addValue(short[] array)
Augment the current computed hash code with the
value array.
|
static boolean |
equalObjects(java.lang.Object o1,
java.lang.Object o2)
Utility function to make it easy to compare two, possibly null, objects.
|
boolean |
equals(java.lang.Object obj) |
int |
hashCode()
Get the currently computed hash code value.
|
java.lang.String |
toString() |
public int addValue(java.lang.Object obj)
obj - value being addedpublic int addValue(boolean b)
b - value being addedpublic int addValue(byte i)
i - value being addedpublic int addValue(char i)
i - value being addedpublic int addValue(short i)
i - value being addedpublic int addValue(int i)
i - value being addedpublic int addValue(float f)
f - value being addedpublic int addValue(double f)
f - value being addedpublic int addValue(java.lang.Object[] array)
array - value being addedpublic int addValue(boolean[] array)
array - value being addedpublic int addValue(byte[] array)
array - value being addedpublic int addValue(char[] array)
array - value being addedpublic int addValue(short[] array)
array - value being addedpublic int addValue(int[] array)
array - value being addedpublic int addValue(float[] array)
array - value being addedpublic int addValue(double[] array)
array - value being addedpublic static boolean equalObjects(java.lang.Object o1,
java.lang.Object o2)
o1 - first objecto2 - second objectpublic int hashCode()
hashCode in class java.lang.Objectpublic boolean equals(java.lang.Object obj)
equals in class java.lang.Objectpublic java.lang.String toString()
toString in class java.lang.Object