I need to have a Set (HashSet) such that if I insert a pair (a, b) and if (b, a) is already in the set, the insertion would just be ignored. How to
final class Pair {
private final Set elements = new LinkedHashSet();
Pair(T a, T b) {
elements.add(a);
if (!elements.add(b))
throw new IllegalArgumentException();
}
@Override
public int hashCode() {
return elements.hashCode();
}
@Override
public boolean equals(Object obj) {
if (obj == this)
return true;
if (!(obj instanceof Pair>))
return false;
return elements.equals(((Pair>) obj).elements);
}
}