In this article we will discuss how to merge an array in a HashSet

HashSet has a member function addAll() that can add a collection into to a HashSet. But this Collection should be of Same Type as of HashSet or of its Base Class.

 

It will add the each element from given collection to HashSet one by one.

But an Array is not a Collection i.e.

How to Merge an Array in HashSet?

For this we need to first convert our array into a Collection i.e. a List. We can do this using Arrays asList() function i.e.

It returns a fixed size List with random access provision. asList() basically acts as an adapter for using non collection based data structure in Collection based APIs.

Now we can easily merge this given List into a HashSet using addAll() function i.e.

Output: