Java HashSet

HashSet implements the set interface. It uses the hash table to make the collection to store different data types. The hash set is the unordered collection of different data types. In other words, we can say that the hash set doesn’t maintain the order of insertion of the elements. The HashSet contains unique elements only. The duplicate elements inserted into the HashSet are considered only once in the collection. It can also contain null values. The indexing in the HashSet is done according to the Hash value of the element inserted into the HashSet.

The default value of the initial position is 16, and the load factor is 0.75. Load factor and initial capacity are two factors which affect the performance of the Hash Table.

Initial Capacity: It is the number of buckets in the Hash table. If the current size is full, then the number of buckets is automatically increasing.

Load Factor: It decides when it increases the capacity of the Hash Map.

Load Factor= Number of stored elements in the table/ Size of Hash Table.

Time Complexity of Hash Set:

HashSet uses the data structure is HashTable. The Average time complexity of HashSet for add(), remove(), contains() methods operation is O(1) time.

Internal working of HashSet:

The classes of the set interface are internally back up by Map. HashMap used to store the object internally in HashSet. When we enter the value in HashMap, then we need a key-value pair, but in HashSet, we need only value.

Implementation:

Constructor

  1. HashSet (): It constructs the default HashSet.
  2. HashSet (Collection c): It initializes the HashSet using the elements of the collection.
  3. HashSet (int capacity): This initializes the capacity of the HashSet. The capacity of HashSet increased automatically when elements added in Hash set.
  4. HashSet (int capacity, float Loadfactor): This initializes the capacity of the HashSet and the load factor of the HashSet from its parameters. The load factor enters in float type and capacity at the given integer value type.

Methods

  1. add (Element e): It is used to add the specific element. If the element is present then returns false.
  2. clear (): It is used to remove all the elements of the set.
  3. contains (object o): It returns true when the elements found in the set.
  4. remove (object o): It is used to delete the element if element present in the set.
  5. iterator (): It is used to return the iterator in the set.
  6. isEmpty (): This method is used to check the set is empty or not.
  7. int size (): This method is used to return the size of the set.
  8. Object clone (): This method is used to create the same copy of the set.

    Traversing

  • Using iterator: The iterator () method is to get an iterator over the element in the set. There is no particular order for returning the element.

Example:

Output:

  • Using for-each loop:

Example:

Output:

  • Using forEach ():

Example:

Output:

Difference between HashSet and TreeSet

HashSet TreeSet
1. Performance is faster. 1. Performance is slower for most of the general operation, i.e., add, remove and search.
2. There is no particular order for an element. 2. Tree set provides the order for elements.
3. For comparing, it uses the equals () method. 3. For comparing, it uses the compareTo () method.
4. The data structure for the hash set is HashTable. 4. The Data Structure for TreeSet is Red-Black Tree.
5. It allows only one null element. 5. It doesn’t allow null element.
6. Implemented using HashMap. 6. Implemented using TreeMap.

add () method:

Output:

Example of clear () method:

Output:

contains () method

Output:

remove () method

Output:

isEmpty () method:

Output:

size () method:

Output:

clone () method:

iterator () method:

Output: