首页 > 其他 > 详细

遍历map集合的4种方法

时间:2020-01-19 10:16:28      阅读:90      评论:0      收藏:0      [点我收藏+]
 1 package com.cn.testmap;
 2 
 3 import java.util.HashMap;
 4 import java.util.Iterator;
 5 import java.util.Map;
 6 import java.util.Map.Entry;
 7 
 8 /**
 9  * map的4种便历方法操作
10  * @author lenovo
11  *
12  */
13 
14 public class Maptest {
15 private static Map<String,String> map = new HashMap<String,String>();
16 public static void main(String[] args) {
17     map.put("name", "李四");
18     map.put("age", "30");
19     map.put("sex", "male");
20     map.put("code", "3010");
21     //方法一:通过key取值
22     /*for(String key:map.keySet()){
23         System.out.printf("map key is %s and value is %s",key,map.get(key));
24         System.out.println();
25     }*/
26     //方法二:通过迭代器取值
27     /*Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();
28     Entry<String, String> entry = null;
29     while(iterator.hasNext()){
30         entry = iterator.next();
31         System.out.printf("key is %s and value is %s",entry.getKey(),entry.getValue());
32         System.out.println();
33     }*/
34     //通过entryset
35     /*for(Entry<String, String> entry:map.entrySet()){
36         System.out.printf("key is %s and value is %s",entry.getKey(),entry.getValue());
37         System.out.println();
38     }*/
39     //通过map的value方法实现
40     for(String value : map.values()){
41         System.out.println("value is "+value);
42     }
43 }
44 }

遍历map集合的4种方法

原文:https://www.cnblogs.com/ying1314/p/12211010.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!