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


2. Iterate HashSet in Java using Iterator


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.


1. ArrayList