title: iterate through a hashmap 'in chunks'
body: <p>I need to iterate through a hashmap with 5000 items but after iterating on 500th item I need to do a sleep and then continue the next 500 items. Here is the example stolen from <a href="https://examples.javacodegeeks.com/java-basics/java-map-example/" rel="noreferrer">here</a>. Any help would appreciated.</p>
  
  <pre><code>import java.util.HashMap;
  import java.util.Map;
  
  public class HashMapExample {
  
      public static void main(String[] args) {
          Map vehicles = new HashMap();
  
          // Add some vehicles.
          vehicles.put("BMW", 5);
          vehicles.put("Mercedes", 3);
          vehicles.put("Audi", 4);
          vehicles.put("Ford", 10);
          // add total of 5000 vehicles 
  
          System.out.println("Total vehicles: " + vehicles.size());
  
          // Iterate over all vehicles, using the keySet method.
          // here are would like to do a sleep iterating through 500 keys
          for(String key: vehicles.keySet())
              System.out.println(key + " - " + vehicles.get(key));
          System.out.println();
  
          String searchKey = "Audi";
          if(vehicles.containsKey(searchKey))
              System.out.println("Found total " + vehicles.get(searchKey) + " "
                      + searchKey + " cars!\n");
  
          // Clear all values.
          vehicles.clear();
  
          // Equals to zero.
          System.out.println("After clear operation, size: " + vehicles.size()); 
      }
  }
  </code></pre>