public class maptest {public static void main(String[] args) {
Map<Integer,String> map = new HashMap<Integer, String>();
for (int i = 0; i < 5000000; i++) {
map.put(i, i+"-");
}
System.out.println(map.size());
long start = System.currentTimeMillis();
for (Entry<Integer, String> entry : map.entrySet()) {
/* System.out.println(entry.getKey());
System.out.println(entry.getValue());*/
entry.getKey();
}
long end = System.currentTimeMillis();
System.out.println("entrySet耗时:"+(end-start));
long start1 = System.currentTimeMillis();
/* Map<String, String> map = new HashMap<String, String>();
*/ for (Integer key : map.keySet()) {
map.get(key);
}
long end1= System.currentTimeMillis();
System.out.println("Keyset耗时:"+(end1-start1));
long start2 = System.currentTimeMillis();
Iterator<Entry<Integer, String>> iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Entry<Integer, String> entry = iterator.next();
entry.getKey();
}
long end2 = System.currentTimeMillis();
System.out.println("iterator()耗时:"+(end2-start2));
}
}
第一种map的遍历方式最快