I want to store my data in HashMap<Point[], Double>. I used iteration to assign the data, but when I checked at the end, the number of elements is only 1. I have implemented hashCode() and equals() in the custom class Point.
HashMap<Point[], Double> hmDistOrd = new HashMap<Point[], Double>();
Point[] keyPts = new Point[2];
for (int i=0; i<intersectionPts.size(); i++) {
p1 = intersectionPts.get(i);
for (int j=0; j<intersectionPts.size(); j++) {
p2 = intersectionPts.get(j);
if (!p1.equals(p2)) {
keyPts[0] = p1;
keyPts[1] = p2;
d = p1.distance(p2);
hmDistOrd.put(keyPts, d);
}
}
}
Any hints? Thanks in advance!
You can’t use array as a key, as array has default implementation of
hashCodeandequalsfromObjectsand it doesn’t consider it’s elements.To make it work you had to override
hashCodeandequalsof array, but you can’t do it.You can use
ArrayListinstead, as it implementshashCodeendequalscomparing elements.