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
最后編輯于 :
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。