class Pair { private T first; private T second; public Pair(T x1, T x2) { first = x1; second = x2; } @Override public String toString() { return "(1)" + first + " (2)" + second; } public T getFirst() { return first; } public T getSecond() { return second; } } class UnorderedPair extends Pair { public UnorderedPair(T firstItem, T secondItem) { super(firstItem, secondItem); } public boolean equals(Object otherObject) { if (otherObject == null) return false; else if (getClass() != otherObject.getClass()) return false; else { UnorderedPair otherPair = (UnorderedPair) otherObject; return ( getFirst().equals(otherPair.getFirst()) && getSecond().equals(otherPair.getSecond()) ) || ( getFirst().equals(otherPair.getSecond()) && getSecond().equals(otherPair.getFirst())); } } } public class Main { public static void main(String[] args) { UnorderedPair x1 = new UnorderedPair<>("ABC", "DEF"); UnorderedPair x2 = new UnorderedPair<>("DEF", "ABC"); UnorderedPair x3 = new UnorderedPair<>("DEF", "AAA"); System.out.println(x1.equals(x2));// true System.out.println(x1.equals(x3));// false } }