map的四种遍历
来源:互联网 发布:弹性光网络 编辑:程序博客网 时间:2024/06/05 06:57
1 public static void main(String[] args) { 2 3 // 构建一个Map 初始值为3条数据 4 Map<String, String> map = new HashMap<String, String>(); 5 map.put("1", "xiaqiu"); 6 map.put("2", "pangzi"); 7 map.put("3", "shouzi"); 8 9 //第一种:普遍使用,二次取值10 System.out.println("通过Map.keySet遍历key和value:");11 for (String key : map.keySet()) {12 System.out.println("key= "+ key + " and value= " + map.get(key));13 }14 15 //第二种:通过Iterator迭代器遍历循环Map.entrySet().iterator();16 System.out.println("通过Map.entrySet使用iterator遍历key和value:");17 Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();18 while (it.hasNext()) {19 Map.Entry<String, String> entry = it.next();20 System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());21 }22 23 //第三种:笔者推荐,尤其是容量大时(相对来说 比2好一点 效率高)24 System.out.println("通过Map.entrySet遍历key和value");25 for (Map.Entry<String, String> entry : map.entrySet()) {26 System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());27 }28 29 //第四种30 System.out.println("通过Map.values()遍历所有的value,但不能遍历key");31 for (String v : map.values()) {32 System.out.println("value= " + v);33 }34 }
阅读全文
0 0
- map的四种遍历
- Map的四种遍历
- map的四种遍历
- map的四种遍历
- Map的四种遍历
- map的四种遍历方式,map遍历元素
- 遍历Map四种
- java Map 遍历的四种方法
- 遍历Map的四种方法
- 遍历Map的四种方法
- 遍历Map的四种方法
- java 遍历Map的四种方法
- 遍历Map的四种方法
- 遍历Map的四种方法
- 遍历Map的四种方法
- 遍历Map的四种方法
- 遍历Map的四种方法
- 遍历Map的四种方法
- jq中 html append appendto
- roscpp模式总结
- 信息查询系统总结--C#list的使用
- 顺时针打印矩阵
- 2-SAT
- map的四种遍历
- PHP SOCKET编程 学学
- 弹性布局-flex
- 动态规划:单调递增子序列
- 剑指offer 之 二进制中1的个数
- 试图加载格式不正确的程序。 (异常来自 HRESULT:0x8007000B)
- [thinkPHP5项目实战_04]命名空间的引入
- Linux下升级gcc、gdb工具
- HeadFirst设计模式之继承与组合