赞
踩
java中 map的遍历方式
- Set<Map.Entry<String, Integer>> entries = map.entrySet();
- for ( Map.Entry<String, Integer> e:entries ) {
- System.out.println(e.getKey());
- System.out.println(e.getValue());
- }
- for (String s : map.keySet() ) {
- System.out.println(s);
- System.out.println(map.get(s));
- }
- Set<Map.Entry<String, Integer>> entrySet = map.entrySet();
- Iterator<Map.Entry<String, Integer>> iterator = entrySet.iterator();
- while (iterator.hasNext()){
- Map.Entry<String, Integer> next = iterator.next();
- System.out.println(next.getKey());
- System.out.println(next.getValue());
- }
- public class MapTest {
- public static void main(String[] args) {
- Map<String,Integer> map = new HashMap<>();
- map.put("张三",12);
- map.put("李四",100);
- map.put("王五",7);
- // entryset
- Set<Map.Entry<String, Integer>> entries = map.entrySet();
- for ( Map.Entry<String, Integer> e:entries ) {
- System.out.println(e.getKey());
- System.out.println(e.getValue());
- System.out.println("111111");
- }
- System.out.println();
- // 挨个获取key然后遍历
- for (String s : map.keySet() ) {
- System.out.println(s);
- System.out.println(map.get(s));
- System.out.println("222222");
- }
- System.out.println();
- // 先entryset在迭代器
- Set<Map.Entry<String, Integer>> entrySet = map.entrySet();
- Iterator<Map.Entry<String, Integer>> iterator = entrySet.iterator();
- while (iterator.hasNext()){
- Map.Entry<String, Integer> next = iterator.next();
- System.out.println(next.getKey());
- System.out.println(next.getValue());
- System.out.println("3333333");
- }
- }
- }
好了,这次的文章就到这里,喜欢的同学可以点赞收藏,遇到问题,可以评论,或者留言,我一定会第一时间给到回馈,感谢观看!!
注:本文为本人学习时心得分享,有讲错或者需要改正的地方,请指正,我会虚心接受
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。