You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This class implements the Set interface, backed by a hash table (actually a HashMap instance). Permits the null element. The class also offers constant time performance for the basic operations like add, remove, contains, and size functions.
Important Points about the HastSet
HashSet doesn’t maintain any order, the elements would be returned in any random order.
Duplicates are not allowed.
Allows a null value to be present.
HashSet is non-synchronized, can be synchronized explicitly.
Constructor
Constructor
Description
Set<E> hs = new HashSet<E>();
Constructs a new, empty set; the backing HashMap instance has default initial capacity (16) and load factor (0.75)
Set<E> hs = new HashSet<E>(Collection C);
Constructs a new set containing the elements in the specified collection.
Set<E> hs = new HashSet<E>(int initialCapacity);
Constructs a new, empty set; the backing HashMap instance has the specified initial capacity and default load factor (0.75).
Methods
Constructor
Description
public boolean add(E e)
Adds the specified element to this set if it is not already present.
public boolean remove(Object o)
Removes the specified element from this set if it is present.
public boolean contains(Object o)
Returns true if this set contains the specified element.
public boolean isEmpty()
Returns true if this set contains no elements.
public int size()
Returns the number of elements in this set (its cardinality).