Map遍历的三种方式
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class Test {
public static void main(String[] args) {
Map<String,Object> map = new HashMap<>();
map.put("a", "this is a");
map.put("b", "this is b");
map.put("c", "this is c");
map.put("d", "this is d");
/* for (Map.Entry<String, Object> entry : map.entrySet()) {
System.out.println(entry.getKey());
System.out.println(entry.getValue());
}*/
/*for (String key : map.keySet()) {
System.out.println(key);
System.out.println(map.get(key));
}*/
/*Iterator<Map.Entry<String,Object>> it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, Object> entry = it.next();
System.out.println(entry.getKey());
System.out.println(entry.getValue());
}*/
}
}
这篇博客详细介绍了在Java中遍历Map的三种常见方法:通过entrySet()、keySet()和iterator()。示例代码展示了如何使用这些方法分别获取并打印Map的键和值,对于理解和操作Map的数据非常有帮助。
3万+

被折叠的 条评论
为什么被折叠?



