How to Iterate HashSet in java

In this post we will see how to iterate HashSet in java. We can iterate HashSet either using
1. For loop
2. Iterator.

We will see examples for all these methods.

1. Iterate HashSet in Java using For Loop

Ouput

2. Iterate HashSet in Java using Iterator

Ouput

Please Note

You will notice that the Elements from hashSet when printed are not in the Order in which we added them.
HashSet doesn’t preserve the order of the elements in which they are inserted.

Reference

1. ArrayList

It's only fair to share...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn