5
我需要迭代通過一個有5000個項目的哈希映射,但在第500個項目迭代後,我需要做一個睡眠,然後繼續下一個500項目。這裏是從here盜取的例子。任何幫助將不勝感激。迭代通過哈希映射'塊'
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());
}
}
這個問題比以前要乾淨得多。 +1 – byxor
@BrandonIbbotson謝謝我正要刪除舊的,但有人發了一個答案,我無法刪除它了。 – PHA
@PHA你總是可以鼓勵回答者在這裏重新發布他的答案,以允許你刪除這個問題。 –