Java HashSet class creates a collection that uses a hash table for storage. HashSet only contains unique elements and it inherits the AbstractSet class and implements the Set interface. Also, it uses a mechanism hashing to store the elements.
```Java
import java.util. * ;
public class MyHashset {
public static void main(String args[]) {
HashSet < String > s = new HashSet();
s.add("a");
s.add("c");
s.add("b");
Iterator < String > itr = s.iterator();
while (itr.hasNext()) {
System.out.println(itr.next());
}
}
}
```
Below are some of the methods of the Java HashSet class:
Method | Description
---- | -----
boolean add(Object o) | Adds the specified element to this set if it is not already present.
boolean contains(Object o) | Returns true if the set contains the specified element.
void clear() | Removes all the elements from the set.
boolean isEmpty() | Returns true if the set contains no elements.
boolean remove(Object o) | Remove the specified element from the set.
Object clone() | Returns a shallow copy of the HashSet instance: the elements themselves are not cloned.
Iterator iterator() | Returns an iterator over the elements in this set.
int size() | Return the number of elements in the set.