Is there a java collection that works like a LinkedHashMap but reflects the order also in equals and hashCode?
-> two Maps with same elements but different order should not be equal and should have different hashCodes.
Solution based on Peter Lawrey’s answer (xAxis is the LinkedHashMap):
hashCode:
...
result = prime * result + ((xAxis == null) ? 0 : xAxis.hashCode() + xAxis.toString().hashCode());
...
equals:
...
if (xAxis == null) {
if (other.xAxis != null) {
return false;
}
} else if (!xAxis.equals(other.xAxis)) {
return false;
} else if (!xAxis.toString().equals(other.xAxis.toString())) {
return false;
}
...
(it based on code generated by eclipse)
You can override the hashCode and equals of HashMap.
Since HashMap has no order, this is very dangerous but you can do it if you want. You might find that comparing the toString() of each is all you really need.
BTW: To get an idea of how many different orders you can have for a keys of a HashMap, this blog post gives you an idea using HashSet (which uses the same code)
http://vanillajava.blogspot.co.uk/2011/09/order-of-elements-in-hash-collection.html