A general solution is to wrap the array in a method that does implement equals
(and hashCode
and perhaps compare
, possibly toString
and other methods that might make sense) as you wish:
public final class IntArrayWrapper {
private final IntArrayWrapper[] values;
public IntArrayWrapper(int... values) {
if (values == null) {
throw new NullPointerException();
}
this.values = values;
}
@Override public boolean equals(Object obj) {
if (!(obj instanceof IntArrayWrapper)) {
return false;
}
IntArrayWrapper other = (IntArrayWrapper)obj;
return java.util.Arrays.equals(this.values, other.values);
}
@Override public int hashCode() {
return java.util.Arrays.hashCode(values);
}
public int[] getValues() {
return values;
}
[...]
}
In this specific case, using arrays to contain certain fixed data values is poor design. Do it properly:
public final class Point {
private final int x;
private final int y;
private final int z;
public static Point of(int x, int y, int z) {
return new Point(x, y, z);
}
private Point(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
@Override public boolean equals(Object obj) {
if (!(obj instanceof Point)) {
return false;
}
Point other = (Point)obj;
return
this.x == other.x &&
this.y == other.y &&
this.z == other.z;
}
@Override public int hashCode() {
int hash;
hash = x;
hash = hash*41+y;
hash = hash*41+z;
return hash;
}
[...]
}