I've stumbled across some code that is broadly along the following lines, but cannot for the life of me fathom why the author is attempting to remove bar from bars before then adding it:
import java.util.Set;
import java.util.HashSet;
class Foo {
private final Set<Object> bars = new HashSet<>();
public void addBar(final Object bar) {
bars.remove(bar); // WHY ????
bars.add(bar);
}
public Object[] getBars() {
return bars.toArray(new Object[0]);
}
}
All that I can come up with is that it's in anticipation of (or legacy from) a different Set implementation that's sensitive to insertion order, such as java.util.LinkedHashSet (although there's nothing about the code or use case that suggests insertion order would be of any interest) - but perhaps there's something else I'm missing?