首页 > 其他 > 详细

遍历Map集合

时间:2014-04-04 11:48:08      阅读:520      评论:0      收藏:0      [点我收藏+]

1:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package Test15;
 
import java.util.HashMap;
 
import java.util.HashSet;
 
import java.util.Iterator;
 
import java.util.Map;
 
import java.util.Set;
 
import java.util.Map.Entry;
 
//Map集合遍历
 
public class Test {
 
    private Map<String, String> map = new HashMap<String, String>();
 
    public static void main(String[] args) {
 
       Test t=new Test();
 
       t.setValue();
 
       t.method3();
 
    }
 
    //为map赋值
 
    public void setValue(){
 
       map.put("name", "张三");
 
       map.put("sex", "男");  
 
    }
 
    //方法1
 
    public void method1(){
 
       Set<String> set = new HashSet<String>();
 
       set = map.keySet(); // 获得map中所有Key
 
       for (String s : set) {
 
           System.out.println(map.get(s));
 
       }
 
    }
 
    //方法2
 
    public void method2(){
 
       Iterator<String> iterator=map.keySet().iterator(); //返回所有key
 
       while(iterator.hasNext()){
 
           String key=iterator.next();
 
           System.out.println(key+": "+map.get(key));
 
       }
 
    }
 
    //方法3
 
    public void method3(){
 
       Set<Entry<String,String>> set= map.entrySet();  //返回:key和value的封装
 
       for(Entry entry:set){
 
           System.out.println(entry.getKey()+":"+entry.getValue());
 
       }
 
    }
 
}

 

遍历Map集合,布布扣,bubuko.com

遍历Map集合

原文:http://www.cnblogs.com/gdds/p/3643943.html

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