 <p>A set by definition doesn't contain any duplicates. A set determines if two items are alike, by using either the objects equals()/compareTo(..) method or by using a Comparator. If you only want unique items in your set, implementing the Comparable interface and overriding equals() is what you want to do. BUT in your case, you're only interested in objects in unique groups, so it's then better to create a custom Comparator for the occasion, which you then supply to the Set, telling it to use it, instead of "natural ordering". </p> <pre><code>Set&lt;MyThing&gt; myThings = new TreeSet&lt;&gt;(new Comparator&lt;MyThing&gt;() { @Override public int compare(MyThing o1, MyThing o2) { return o1.getGroupId() - o2.getGroupId(); } }); myThings.addAll(Arrays.asList(myArray)); </code></pre> <p>After creating the set, you add your entire array into it, using the convinience method addAll(..).</p> <p>(How the comparator sorts your objects is completely up to you to decide.)</p>