I'm using a LinkedHashSet
to implement a set ordered by entry. If a duplicate element is added, it should go at the end and the entry already in the set should be removed.
I'm debating whether it's worth it to check set.contains(entry)
before I call set.remove(entry)
? Essentially does checking contains()
each time offer any time/space benefit over just blindly calling remove()
each time?
Set<Integer> set = new LinkedHashSet<>();
// The below
set.remove(entry);
set.add(entry);
// Versus the below
if (set.contains(entry)) {
set.remove(entry);
}
set.add(entry);