Java遍历Map对象的四种方法(Map泛型)
添加map模拟数据
Map<String,Object> map = new HashMap<>();
for (int i = 0; i < 10; i++) {
map.put("syx" + i, "123" + i);
}
1.使用entrySet遍历
- 遍历map数据
for (Map.Entry<String, Object> m : map.entrySet()){
System.out.print("key: " + m.getKey());
System.out.print("value: " + m.getValue());
}
2.使用keySet、values遍历
- 遍历map数据中的key
for (String m : map.keySet()){
System.out.print("key: " + m);
}
- 遍历map数据中的value
for (String m : map.values()){
System.out.print("values: " + m);
}
3.使用Iterator遍历
- 遍历map数据
Iterator<Map.Entry<String, Object>> iterator = map.entrySet().iterator();
while (iterator.hasNext()){
Map.Entry<String, Object> entry = iterator.next();
System.out.print("key: " + entry.getKey());
System.out.print("value: " + entry.getValue());
}
4.使用Lambda遍历
- 遍历map数据
map.forEach((key,value)->{
System.out.print("key: " + key);
System.out.print("value: " + value);
});
5.总结
同时遍历10000条数据时的用时
随机执行5次
方法 | 第一次(ms) | 第二次(ms) | 第三次(ms) | 第四次(ms) | 第五次(ms) |
---|---|---|---|---|---|
entrySet用时 | 217 | 192 | 253 | 179 | 222 |
keySet用时 | 88 | 48 | 109 | 41 | 51 |
values用时 | 53 | 63 | 41 | 38 | 43 |
Iterator用时 | 231 | 110 | 65 | 72 | 62 |
lambda用时 | 292 | 438 | 441 | 306 | 272 |
- 同时获取key和value时: Iterator > entrySe > lambda
- 只使用key或value时: keySet、values
。
。
。
。
。
。
。
。
全部代码如下
// 遍历map泛型
Map<String,Object> map = new HashMap<>();
for (int i = 0; i < 10000; i++) {
map.put("syx" + i, "123" + i);
}
/**
* entrySet
* */
//直接遍历出key、value
System.out.println("---------------------1 entrySet Map<String,Object> ");
long startTime = System.currentTimeMillis();
for (Map.Entry<String, Object> m : map.entrySet()){
System.out.print("key: " + m.getKey());
System.out.print("value: " + m.getValue());
}
long endTime = System.currentTimeMillis();
/**
* keySet、values
* */
// 根据key遍历出value
System.out.println("---------------------2 keySet Map<String,Object>---------------------------");
long startTime1 = System.currentTimeMillis();
for (String m : map.keySet()){
System.out.print("key: " + m);
}
long endTime1 = System.currentTimeMillis();
System.out.println("---------------------3 values Map<String,Object>---------------------------");
// 遍历value
long startTime2 = System.currentTimeMillis();
for (Object m : map.values()){
System.out.print("value: " + m);
}
long endTime2 = System.currentTimeMillis();
/**
* Iterator
* */
System.out.println("---------------------4 iterator Map<String,Object>---------------------------");
long startTime3 = System.currentTimeMillis();
Iterator<Map.Entry<String, Object>> iterator = map.entrySet().iterator();
while (iterator.hasNext()){
Map.Entry<String, Object> entry = iterator.next();
System.out.print("key: " + entry.getKey());
System.out.print("value: " + entry.getValue());
}
long endTime3 = System.currentTimeMillis();
System.out.println("---------------------5 Lambda Map<String,Object>---------------------------");
long startTime4 = System.currentTimeMillis();
map.forEach((key,value)->{
System.out.print("key: " + key);
System.out.print("value: " + value);
});
long endTime4 = System.currentTimeMillis();
System.out.println("----------------------------------------");
System.out.println("entrySet用时:" + (endTime-startTime));
System.out.println("keySet用时:" + (endTime1-startTime1));
System.out.println("values用时:" + (endTime2-startTime2));
System.out.println("Iterator用时:" + (endTime3-startTime3));
System.out.println("lambda用时:" + (endTime4-startTime4));