I try to write a program that stores non-equal pair-objects, consisting of 2 Strings. For that matter, the pair (john, bob) is considered equal to (bob, john). My equals and compareTo implementations should work fine. To check whats wrong I let my program output the comparisions made for each new Pair I try to add. Looks like this:
@Override
public boolean equals(Object o){
    if (o==null){
        return false;
    }
    final Pair other = (Pair) o;
    return (this.compareTo(other)==0);
}
@Override
public int compareTo (Pair o){
  if (this.first.equals(o.first)){
      if (this.second.equals(o.second)){
          System.out.println("equal: "+this.first+" "+this.second+" and  " + o.first+" "+o.second);
          return 0;
      }
  }
  else if (this.first.equals(o.second)){
        if (this.second.equals(o.first)){
            System.out.println("equal: "+this.first+" "+this.second+" and  " + o.first+" "+o.second);
            return 0;
        }
  }
    System.out.println(" not equal " +this.first+" "+this.second+" and  " + o.first+" "+o.second);
  return -1;
Exampleinput:
 bob john
 john john
 john john
 john bob
 bob will
 john hohn
If I let this run it will print out the size of the TreeSat after each trial to add a new element. It will also print what is written in the compareTo method. I added comments to specifify my problem.
   equal: bob john and  bob john    //Why comparing the first element at  
                                      all?
1
 not equal john john and  bob john
2
 not equal john john and  bob john
equal: john john and  john john
2
equal: john bob and  bob john
2
 not equal bob will and  bob john
 not equal bob will and  john john
3
 not equal john hohn and  john john    //no comparision of (john hohn) and                                       
 not equal john hohn and  bob will     //(bob john) why?
4
 
    