Friday, August 13, 2021

How to Loop Through HashSet in Java

In this post we’ll see how you can iterate any Set implementation like HashSet in Java. To loop through a HashSet you have following options, you can choose based on your requirement or the Java version you are using.

  1. First option to iterate a HashSet in Java is to use a ForEach loop, if you are just iterating without doing any structural modification (adding or removing element) to the Set then this is the best option to iterate a HashSet.
  2. Using iterator() method which returns an iterator over the elements in the given Set. If you have to remove any element while iterating then use this option otherwise ConcurrentModificationException will be thrown.
  3. Java 8 onwards you also have an option to use forEach statement to iterate a HashSet in Java.

Iterating a HashSet – Java example

Let’s see a Java example code which illustrates all of the options mentioned above to loop through a HashSet.

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class IterateSetDemo {

  public static void main(String[] args) {
    // creating a HashSet
    Set<String> citySet = new HashSet<String>();
    // Adding elements
    citySet.add("London");        
    citySet.add("Tokyo");
    citySet.add("New Delhi");
    citySet.add("Beijing");
    citySet.add("Nairobi");
    
    System.out.println("-- Iterating HashSet - ForEach loop --");
    for(String city : citySet){
      System.out.println("city- " + city);
    }
    
    System.out.println("-- Iterating HashSet - using iterator --");
    Iterator<String> itr = citySet.iterator();
    while(itr.hasNext()){            
      System.out.println("city- " + itr.next());        
    }
    
    System.out.println("-- Iterating HashSet - using forEach statement-- ");
    citySet.forEach((city)->System.out.println("city- " + city));        
  }
}

Output

-- Iterating HashSet - ForEach loop --
city- Beijing
city- New Delhi
city- Nairobi
city- Tokyo
city- London
-- Iterating HashSet - using iterator --
city- Beijing
city- New Delhi
city- Nairobi
city- Tokyo
city- London
-- Iterating HashSet - using forEach statement-- 
city- Beijing
city- New Delhi
city- Nairobi
city- Tokyo
city- London

Recommendations for learning (Udemy Courses)

  1. Java Programming Masterclass Course
  2. Java In-Depth: Become a Complete Java Engineer!
  3. Spring Framework Master Class Course
  4. Complete Python Bootcamp Course
  5. Python for Data Science and Machine Learning

That's all for this topic How to Loop Through HashSet in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!


Related Topics

  1. How to Loop Through a Map in Java
  2. How to Sort HashSet in Java
  3. LinkedHashSet in Java With Examples
  4. How ArrayList Works Internally in Java
  5. Java Collections Interview Questions And Answers

You may also like-

  1. Java ReentrantLock With Examples
  2. How and Why to Synchronize ArrayList in Java
  3. CopyOnWriteArrayList in Java With Examples
  4. Executor And ExecutorService in Java With Examples
  5. Array in Java With Examples
  6. instanceof Operator in Java
  7. Comparing Enum to String in Java
  8. Spring NamedParameterJdbcTemplate Insert, Update And Delete Example

No comments:

Post a Comment