Map获取key值

有两种方法

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());
}
}

打印结果如下:

 

posted @ 2019-04-11 10:21  無玑小姐  阅读(17750)  评论(0编辑  收藏  举报