Map的三种便利方式
package com.toltech.springboot;
import java.util.*;
/**
* Created by Wgs on 2017/10/11
* 遍历集合
*/
public class Test {
public static void main(String[] args) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("tom", 12);
map.put("jack", 21);
System.out.println("方式一");
// 方式一
Set set = map.entrySet();
Iterator iterator = set.iterator();
while (iterator.hasNext()) {
Map.Entry me = (Map.Entry) iterator.next();
Object key = me.getKey();
Object value = me.getValue();
System.out.println("key: " + key + " value: " + value);
}
System.out.println("方式二");
// 方式二
for (String key : map.keySet()) {
System.out.println("key: " + key + " value: " + map.get(key));
}
System.out.println("方式三");
// 方式三
for (Map.Entry<String, Object> entry : map.entrySet()) {
System.out.println("enty " + entry + " key: " + entry.getKey() + " value: " + entry.getValue());
}
}
}
方式一
key: tom value: 12
key: jack value: 21
方式二
key: tom value: 12
key: jack value: 21
方式三
enty tom=12 key: tom value: 12
enty jack=21 key: jack value: 21