Map的五种遍历方法
来源:互联网 发布:php开发实战教程 编辑:程序博客网 时间:2024/06/04 17:59
</pre><p>方法代码所示,其中方法二为最优方法。</p><p></p>import java.util.Iterator;import java.util.Map;import java.util.Map.Entry;import java.util.TreeMap;public class MapDemo {public static void main(String[] args) {Map<Integer, String> map = new TreeMap<Integer, String>();map.put(1, "孙杨");map.put(2, "宁泽涛");map.put(3, "林丹");map.put(4, "杜兰特");map.put(5, "安东尼");map.put(6, "欧文");map.put(7, "德罗赞"); // 方法五System.out.println("================");Iterator<Map.Entry<Integer, String>> t = map.entrySet().iterator();while (t.hasNext()) {System.out.println(t.next());}// 方法四for (Integer i : map.keySet()) {String value = map.get(i);System.out.println(value);}// 方法三System.out.println("方式三");Iterator<Map.Entry<Integer, String>> iterator = map.entrySet().iterator();while (iterator.hasNext()) {<pre name="code" class="java">
Map.Entry<Integer, String> mapTemp = iterator.next();
System.out.println(mapTemp.getKey() + " = " + mapTemp.getValue());
}
// 方法二
System.out.println("方式二");
map.entrySet();
for (Map.Entry<Integer, String> xqsmqsm : map.entrySet()) {
System.out.println(xqsmqsm.getKey() + " = " + xqsmqsm.getValue());
}
// 方法一
System.out.println("方式一");
System.out.println("以下是map的值集合");
for (String str : map.values()) {
System.out.println(str);
}
System.out.println("以下是map的键集合");
for (Integer i : map.keySet()) {
System.out.println(i);
}
}
}
0 0
- Map的五种遍历方法
- java遍历map的五种方法
- Map 遍历 五种方法
- java 遍历map 方法 集合 五种的方法
- java 遍历map 方法 集合 五种的方法
- java 遍历map 方法 集合 五种的方法
- java 遍历map 方法 集合 五种的方法
- Map集合遍历的五种方式
- Map的遍历方法
- map的遍历方法
- map的遍历方法
- Map的遍历方法
- Map的遍历方法
- Map的遍历方法
- map遍历的方法
- Map的遍历方法
- Map的遍历方法
- 遍历map的方法
- xibyu约束scrollerview
- 二分法,递归和非递归方式 实现(Java)
- Integer与int的区别
- ollydbg 调试
- SpringMVC环境快速搭建
- Map的五种遍历方法
- 加载baidu地图报error inflating class com.baidu.mapapi.map.MapView
- 原型模式(重新思考深浅拷贝)
- UVALive 6910 Cutting Tree 并查集
- Aop Joinpoint语法
- Uva 10765 Doves and bombs (点双联通分量 + Block Forest Data Structure)
- Oracle 11g R2 DBA 操作指南(9)
- 使用ckeditor在Ajax 序列化表单提交时不能获取 textarea 的值 解决方案
- 二维码解码器Zbar+VS2012开发环境配置