In this article we will discuss how to merge two HashSets.

HashSet provides a member function addAll() i.e.

It provides a functionality to add a collection into to a HashSet. But this Collection should be of Same Type as of HashSet or of its Base Class.

How addAll() works

It will iterate over the given Collection one by one and add each element individually in the Hashset. Also, if it has added any element then it will return true else returns false.

Let’s see how to merge to HashSets i.e.

Output: