element to be added to this set
true
if this set did not already contain the specified
element
UnsupportedOperationException if the add
operation
is not supported by this set
ClassCastException if the class of the specified element prevents it from being added to this set
NullPointerException if the specified element is null and this set does not permit null elements
IllegalArgumentException if some property of the specified element prevents it from being added to this set
Returns true
if this set contains the specified element.
More formally, returns true
if and only if this set
contains an element e
such that
Objects.equals(o, e)
.
element whose presence in this set is to be tested
true
if this set contains the specified element
ClassCastException if the type of the specified element is incompatible with this set (optional)
NullPointerException if the specified element is null and this set does not permit null elements (optional)
Adds the specified element to this set if it is not already present (optional operation). More formally, adds the specified element
e
to this set if the set contains no elemente2
such thatObjects.equals(e, e2)
. If this set already contains the element, the call leaves the set unchanged and returnsfalse
. In combination with the restriction on constructors, this ensures that sets never contain duplicate elements.The stipulation above does not imply that sets must accept all elements; sets may refuse to add any particular element, including
null
, and throw an exception, as described in the specification for Collection#add Collection.add. Individual set implementations should clearly document any restrictions on the elements that they may contain.