I have a class in java which contains two integers and an array of integers as members and I want to make a hash map with the above object as key. How should i override the equals operator and hashCode() such that the object which have same Integer values as that of members and same entries in the array get the same Hash Code?(or is such a thing even possible) Thanks in Advance.
Asked
Active
Viewed 1,284 times
3
-
3Possible duplicate of [Override equals](http://stackoverflow.com/questions/8180430/how-to-override-equals-method-in-java) – sam Oct 13 '15 at 15:06
-
I wrote this a while back as a way of doing what your are trying to do in a consistant way. Might be useful: http://sourceforge.net/p/tus/code/HEAD/tree/tjacobs/util/PrimaryKey.java – ControlAltDel Oct 13 '15 at 15:11
-
@sam2090 I think this question is asking for how to correct use `equals` and `hashCode` on *arrays*, not on integers. – Clashsoft Oct 13 '15 at 15:12
-
@ControlAltDel Was that "while back" before 1.5? – laune Oct 13 '15 at 15:17
-
@laune I think we were at 1.5 at the time. To be honest, I recall that even when I was coding this up, I didn't think it was such an elegant solution. But I thought I'd mention it. – ControlAltDel Oct 13 '15 at 15:27
-
@sam2090 yes that's what i am looking for.I want to know how to correct equals for the array member of class and then how to implement the corresponding hash function. – Anurag Sanyal Oct 13 '15 at 17:59
-
Your question have multiple answers. Mark as answer the one that you feel helped you the best as a way to appreciate the author and also, for future readers. – sam Oct 13 '15 at 21:26
3 Answers
4
Use java.util.Arrays#equals(int[], int[])
and Arrays.hashCode(int[])

erosb
- 2,943
- 15
- 22
-
Just a cautioning remark that Arrays.equals and Arrays.hashCode cannot be applied to *arbitrary* arrays: check Arrays.deepEquals and Arrays.deepHashCode. – laune Oct 13 '15 at 15:35
-
2
To calculate a hashcode for the int array you can use java.util.Arrays.hashcode(int[])
.
If you look at its implementation:
public static int hashCode(int a[]) {
if (a == null)
return 0;
int result = 1;
for (int element : a)
result = 31 * result + element;
return result;
}
you can derive an idea how to calculate a hash code for your class which should be based on the values of your two integers and the integer array:
public class MyClass {
private int a, b;
private int[] array;
public int hashCode() {
return (31 * (31 * Arrays.hashCode(array) + a)) + b;
}
To equals implementation can look like:
public int equals(Object o) {
if (o instanceof of MyClass) {
MyClass m = (MyClass)o;
return m.a == a && m.b == b && Arrays.equals(m.array, array);
}
else
return false;
}

wero
- 32,544
- 3
- 59
- 84
0
You can check for array equality using the java.util.
Arrays
class:
int[] array1 = { 1, 2, 3 };
int[] array2 = { 1, 2, 3 };
boolean equal = Arrays.equals(array1, array2) // --> true
To calculate the hash code of an array, the same class can help you out as well:
int hash = Arrays.hashCode(new int[] { 1, 2, 3 })
Note that the class has overloaded methods for all array types - including Object[]
.

Clashsoft
- 11,553
- 5
- 40
- 79