![]() |
VOOZH | about |
In Java, we can iterate over a HashSet without using an Iterator. For this, we need two methods. We can directly loop through the elements of the HashSet.
In this article, we will discuss the two methods to iterate over a HashSet without using Iterator.
Syntax:
for (Datatype variable : Set) {
//Write code using variable
}In this method, we will iterate over the HashSet using forEach loop. Below is the implementation:
rust python c++ java javascript
We can use forEach method which is a simple way to iterate over a set. And it can be written in single line. It is similar to ForEach loop. The values which are present in the set is stored in random order so if we iterate over it, the values will come in the random order.
Syntax:
collection.forEach(element -> {
// Write code using element
});
rust python c++ java javascript