title: Compare different object types with comparator
body: <p>I need to write a Comparator that take an object A of type A and an object B of type B. The two object are not an extention of a common object. They are really diffferent, but I need to compare this two object by common fields in it. I have to use the comparator interface because the objects are stored in Set and after I have to do operations with CollectionUtils. I googled a little bit arround and I find solutions with Comparator but only with the same type. </p>
  
  <p>I tried to implement a think in this direction but I don't know If I'm very on the correct way.</p>
  
  <pre><code>public class MyComparator implements Comparator&lt;A&gt;, Serializable {
  
    private B b;
  
    public MyComparator(B b){
         this.b = b;
    }
  
    @Override
    public int compare(A old, A otherOne) {
      int value = 0;
      if (!old.getField().equals(b.getField())) {
          value = 1;
      }
      return value;
    }
  }
  </code></pre>
  
  <p>It's possible that an answer is always given, but I did not found the right words to search in Google. Have someone suggestions?</p>
  
  <p>Txs </p>
  
  <p>P.S: I add the two objects in diferent Set: </p>
  
  <pre><code>TreeSet&lt;A&gt; setA = new TreeSet&lt;A&gt;(myComparator);
  TreeSet&lt;B&gt; setB = new TreeSet&lt;B&gt;(myComparator);
  </code></pre>
  
  <p>and after I would do thinks like this:</p>
  
  <pre><code>TreeSet&lt;??????&gt; retain = CollectionUtils.retainAll(setA, setB);
  TreeSet&lt;??????&gt; remove = CollectionUtils.removeAll(setA, setB);
  </code></pre>