有两种方法
public static void test4(){
Map<String, Object> map = new HashMap<>();
map.put("key1",1);
map.put("key2",2);
//第一种keySet()
System.out.println("map.keySet():"+map.keySet());
Set<String> strings = map.keySet();
Iterator<String> iterator = map.keySet().iterator();
while (iterator.hasNext()){
System.out.println("iterator:"+iterator.next());
}
//第二种
System.out.println("map.entrySet():"+map.entrySet());
Iterator<Map.Entry<String, Object>> iterator1 = map.entrySet().iterator();
while (iterator1.hasNext()){
System.out.println("iterator1:"+iterator1.next());
}
//----------
for (Map.Entry<String, Object> entry : map.entrySet()){
System.out.println("key:"+entry.getKey()+";value:"+entry.getValue());
}
}
打印结果如下: