 <p>You can use Object instant of StudentUnion. Object will accept all classes. </p> <pre><code>public class CommonComparator implements Comparator&lt;Object&gt; { public int compare(Object s1, Object s2){ .... return result // 1 or 0; } } </code></pre>