Here I am writing a way to implement Hashset. HashSet creates hashtable to store the data. As it implements Set interface so it doesnt contains duplicate elements. It also extends AbstractSet class.
We can implement HashSet by using different constructors :-
HashSet( ) :- This is the by default constructor.
HashSet(Collection c) :- Here hash set is initialized with collection c.
HashSet(int capacity) :- Here hashset is created with given integer capacity but capacity increase with increase of the elements.
We add elements by using add(Object obj) method. We can clear hashset object using clear() method. We can create copy of the object using clone() method. We can also implement Iterator in hashset.
Below is the implementation :-
HashSet myHashSet = new HashSet();
// add elements to the hash set