java 循环遍历 map
Java 中可以使用以下方法遍历 Map:
- for-each loop:
for (Map.Entry<Key, Value> entry : map.entrySet(.html)) {
Key key = entry.getKey();
Value value = entry.getValue();
// ...
} - Iterator:
Iterator<Map.Entry<Key, Value>> it = map.entrySet().iterator();
while (it.hasNext(.html)) {
Map.Entry<Key, Value> entry = it.next();
Key key = entry.getKey();
Value value = entry.getValue();
// ...
}- Stream API:
map.forEach((key, value.html) -> {
// ...
}); - Java 8 forEach + Lambda:
map.entrySet().forEach(entry -> {
Key key = entry.getKey();
Value value = entry.getValue();
// ...
}); 版权声明:本文为原创文章,版权归 戴老师的博客 所有,转载请联系博主获得授权。
本文地址:https://www.tushu.info/archives/archives-java-xun-huan-bian-li-map.html
如果对本文有什么问题或疑问都可以在评论区留言,我看到后会尽量解答。