遍历Map的四种方法详解编程语言
public static void main(String[] args) {
Map String, String map = new HashMap String, String
map.put( 1 , value1 );
map.put( 2 , value2 );
map.put( 3 , value3 );
//第一种:普遍使用,二次取值
System.out.println( 通过Map.keySet遍历key和value: );
for (String key : map.keySet()) {
System.out.println( key= + key + and value= + map.get(key));
}
//第二种
System.out.println( 通过Map.entrySet使用iterator遍历key和value: );
Iterator Map.Entry String, String it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry String, String entry = it.next();
System.out.println( key= + entry.getKey() + and value= + entry.getValue());
}
//第三种:推荐,尤其是容量大时
System.out.println( 通过Map.entrySet遍历key和value );
for (Map.Entry String, String entry : map.entrySet()) {
System.out.println( key= + entry.getKey() + and value= + entry.getValue());
}
//第四种
System.out.println( 通过Map.values()遍历所有的value,但不能遍历key );
for (String v : map.values()) {
System.out.println( value= + v);
}
}
原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/20543.html
c相关文章
- java map 转string_java-将Map <String,Object>转换为Map <String,String>
- 二叉树的层序遍历(两种方法实现)
- java中数组遍历的三种方式
- 二叉树的遍历
- java8的map遍历几种方式[通俗易懂]
- 遍历map的几种方法?_hashmap如何遍历
- C++ map遍历(简单易记忆)[通俗易懂]
- 【Flutter】Dart 数据类型 Map 类型 ( 创建 Map 集合 | 初始化 Map 集合 | 遍历 Map 集合 )
- 【Kotlin】集合操作总结 ( List 集合 | MutableList 集合 | List 集合遍历 | Set 集合 | MutableSet 集合 | Map 集合 | 可变 Map集合 )
- Java遍历Map对象的四种方式详解编程语言
- struts2:OGNL表达式,遍历List、Map集合;投影的使用详解编程语言
- JAVA遍历Map的方法详解编程语言
- Redis快速遍历Map的技巧(redis 遍历map)
- Oracle遍历查询打开新的查询模式(oracle关于遍历查询)
- 解决Redis频繁修改Map难题(redis频繁修改map)
- 灵活运用Redis如何实现设置多个Map(redis设置多个map)
- 学会调整Redis中Map容量的设置(redis设置map大小)
- 使用Redis进行Map设置(redis设置map)
- php若干单维数组遍历方法的比较
- java中关于Map的三种遍历方法详解
- JAVA遍历map的几种实现方法代码