Remove elements compared with other elements in a Set based on a condition
In the case where you just want to find the best tuples, the code can be much simpler:


find the Tuple with the best t for each value of itv (sounds like a job for Map)
put the best bunch of Tuple into a Set, and return it


public static Set&lt;Tuple&gt; bestTuples(Set&lt;Tuple&gt; set) {

    Map&lt;Integer, Tuple&gt; bestTuplesByItv = new HashMap&lt;Integer, Tuple&gt;();
    Tuple bestTupleSoFar;
    for (Tuple t: set) {
        if ((bestTupleSoFar = bestTuplesByItv.get(t.itv)) == null || t.t &gt; bestTupleSoFar.t) {
            bestTuplesByItv.put(t.itv, t);
        }
    }

    Set&lt;Tuple&gt; bestTuples = new HashSet&lt;Tuple&gt;();
    bestTuples.addAll(bestTuplesByItv.values());

    return bestTuples;

}


This should also be faster - your solution has some nested loops, O(n^2), but the suggestion above just iterates through the entire set once. My gut tells me that removing things from a Set is slower than just making a new set with the things we want, but I'm not sure about HashSet.

Since the values for the Map are coming from a HashSet, all the values are guaranteed to have unique hash value. Since that's the case, we don't need a LinkedHashMap - a plain HashMap will work just fine (LinkedHashMap creates linked lists to deal with hash collisions - but that's unnecessary as we won't have any).

Your code seems to be mutating the Set that is passed in as a parameter though - if that's desired behaviour, it's a trivial modification from the code above - just clear the values in set and add the values we want before returning:

public static Set&lt;Tuple&gt; compareAndRemove(Set&lt;Tuple&gt; set) {

    Map&lt;Integer, Tuple&gt; bestTuplesByItv = new HashMap&lt;Integer, Tuple&gt;();
    Tuple bestTupleSoFar;
    for (Tuple t: set) {
        if ((bestTupleSoFar = bestTuplesByItv.get(t.itv)) == null || t.t &gt; bestTupleSoFar.t) {
            bestTuplesByItv.put(t.itv, t);
        }
    }

    set.clear();
    set.addAll(bestTuplesByItv.values());

    return set;

}