Engineering Full Stack Apps with Java and JavaScript
Set is a collection that models the mathematical set abstraction and contains no duplicate elements.
Sets contain no pair of elements e1 and e2 such that e1.equals(e2), and at most one null element.
Some implementations may prohibit null elements.
The Set interface places additional stipulations on:
the contracts of the add, equals and hashCode methods that are inherited from Collection interface.
the constructors, that all constructors must create a set that contains no duplicate elements.
Great care must be exercised if mutable objects are used as set elements:
The behavior of a set is not specified if the value of an object is changed in a manner that affects equals comparisons.
It is not permissible for a set to contain itself as an element.
Important methods of Set interface
add(E e)
Adds the specified element to this set if it is not already present
If this set already contains the element, the call leaves the set unchanged and returns false.
spliterator()
Creates a Spliterator over the elements in this set.
This was introduced in Java 8.
Other methods include add(E e), addAll(Collection<? extends E> c), clear(), contains(Object o), containsAll(Collection<?> c), equals(Object o), hashCode(),isEmpty(),iterator(),remove(Object o),removeAll(Collection<?> c),retainAll(Collection<?> c),size(),toArray(),toArray(T[] a).