The Java Set API has the ``add`` method return true iff the element was already in the set so that you can use it in this type of situation rather than testing containment and conditionally inserting the element for better performance.