I need a hashCode implementation in Java that ignores the order of fields in my Edge class. It should be possible that Node may first be Node second, and Node may be second.
Here is my method depends on the order:
public class Edge { private Node first, second; @Override public int hashCode() { int hash = 17; int hashMultiplikator = 79; hash = hashMultiplikator * hash + first.hashCode(); hash = hashMultiplikator * hash + second.hashCode(); return hash; } }
Is there a way to calculate a hash that is the same but unique for the following Edges?
Node n1 = new Node("a"); Node n2 = new Node("b"); Edge ab = new Edge(n1,n2); Edge ba = new Edge(n2,n1);
ab.hashCode() == ba.hashCode() must be true .
source share