I'm used to programming in PHP and now I'm studing Java, and the whole Collections API is a whole new thing for me.
I searched and got to know some ways to iterate over a HashSet, like using:
- Java 8 forEach and lambda expression.
- iterator().
- iterator() and Java 8 forEachRemaining() method.
- simple for-each loop.
Source and many others easily found in the internet.
But what's the best way to iterate over it or what's the difference between those, like which one should I use in different scenarios.
iterator()andhasNext()andnext()on it is called anyways. Just use whatever fits to the content of your loop the best.