3 ways to loop over Set or HashSet in Java? Examples
Since Set interface or HashSet class doesn't provide a get()
method to retrieve elements, the only way to take out elements from a
Set is to iterate over it by using Iterator, or loop over Set using
advanced for loop of Java 5. You can get the iterator by calling the iterator()
method of Set interface. This method returns an iterator over the
elements in the sets but they are returned in no particular order, as
Set doesn't guarantee any order. Though individual Set implementations
e.g. LinkedHashSet or TreeSet can
impose ordering and in such iterator will return elements on that
order. You can only traverse in one direction using iterator i.e. from
first to last elements, there is no backward traversing allowed as was
the case with List interface and ListIterator. Similarly, if you use advanced for loop, then also you can traverse in only one direction.
In this tutorial, you will learn both ways to traverse over Set or HashSet in Java i.e. by using both Iterator and enhanced for loop. Btw, Java 8 also introduced a new to loop over set, that is by using the forEach() method if you are lucky to be running on Java 8 then you can use that as well.
This will print all the stocks from the setOfStocks into the console.
There is another forEach() method defined in the Stream class from new JDK 8 Stream API, See 10 ways to use forEach in Java 8 for more examples.
Here is the summary of all three ways to iterate over HashSet or any Set in Java:
In this tutorial, you will learn both ways to traverse over Set or HashSet in Java i.e. by using both Iterator and enhanced for loop. Btw, Java 8 also introduced a new to loop over set, that is by using the forEach() method if you are lucky to be running on Java 8 then you can use that as well.
Iterating over Set using Iterator
Here are the steps to traverse over as Set using Iterator in Java:- Obtain the iterator by calling the iterator() method.
- You can use while or for loop along with hasNext(), which return true if there are more elements in the Set.
- Call the next() method to obtain the next elements from Set.
Iterator<String> itr = setOfStocks.iterator(); // traversing over HashSet System.out.println("Traversing over Set using Iterator"); while(itr.hasNext()){ System.out.println(itr.next()); }
Loop over HashSet using for loop
There are no particular steps to loop over Set using enhanced for loop, as you just need to use the Set as per loop construct as shown below:for(String stock : setOfStocks){ System.out.println(stock); }
This will print all the stocks from the setOfStocks into the console.
Traversing over HashSet using forEach() method
This method is only available from Java 8 onwards. This forEach() method belongs to Iterable interface and since Set implements that you can use this to traverse over Set as shown below:public class HashSetLooperJava8{ public static void main(String args[]) { // Creating and initializing an HashSet for iteration Set<String> setOfBooks = new HashSet<>(); setOfBooks.add("Effective Java"); setOfBooks.add("Clean Code"); setOfBooks.add("Refactoring"); setOfBooks.add("Head First Java"); setOfBooks.add("Clean Coder"); // iterating over HashSet using forEach() method in Java 8 setOfBooks.forEach(System.out::println); } } Output: Clean Code Effective Java Head First Java Clean Coder Refactoring
There is another forEach() method defined in the Stream class from new JDK 8 Stream API, See 10 ways to use forEach in Java 8 for more examples.
Here is the summary of all three ways to iterate over HashSet or any Set in Java:

No comments:
Post a Comment