第一种 map.entrySet()
public class Main1 {
public static void main(String[] args){
Map<String, String> map = new HashMap();
map.put("key", "demo");
map.put("key1", "demo1");
for (Map.Entry<String, String> a : map.entrySet()) {
System.out.println(a.getKey() + "=======" + a.getValue());
}
}
}
第二种 Iterator迭代器
public class Main1 {
public static void main(String[] args) {
Map<String, String> map = new HashMap();
map.put("key", "demo");
map.put("key1", "demo1");
map.put("key2", "demo2");
Iterator iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry entry = (Map.Entry) iterator.next();
System.out.println(entry.getKey() + "=====" + entry.getValue());
}
}
}
第三种 个人认为巨笨的方法
public class Main1 {
public static void main(String[] args) {
Map<String, String> map = new HashMap();
map.put("key", "demo");
map.put("key1", "demo1");
map.put("key2", "demo2");
Iterator iterator = map.keySet().iterator();
while (iterator.hasNext()) {
//取出key
String key = iterator.next().toString();
System.out.println(key);
//通过key拿到value
String str = map.get(key);
System.out.println(str);
}
}
}
————————————————
版权声明:本文为CSDN博主「奈何_smail」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_42081445/article/details/105126856