posts - 397,comments - 0,views - 25332

Aop集合的第一种遍历方式:通过键找值的方式

Map集合中的方法

Set<K> keyset()返回此映射中包含的键的set视图。

实现步骤:

  使用Map集合中的方法keySet(),把ap集合所有的key取出来,存储到一个set集合中

       遍历set集合,获取Aap集合中的每一个key

  通过Aap集合中的方法get(key,通过key找到value  

 

图:

 

 

 

遍历:迭代器

复制代码
HashMap<String, Integer> map = new HashMap<>();
        map.put("张三",168);
        map.put("李四",12);
        map.put("王五",45);
        System.out.println(map);
        Set<String> set = map.keySet();
        Iterator<String> iterator = set.iterator();
        while (iterator.hasNext()){
            String key = iterator.next();
            Integer integer = map.get(key);
            System.out.println(key+integer);
        }
复制代码

增强for

复制代码
HashMap<String, Integer> map = new HashMap<>();
        map.put("张三",168);
        map.put("李四",12);
        map.put("王五",45);
        System.out.println(map);

for (String key :set) {
            Integer integer = map.get(key);
            System.out.println(key+integer);
        }
复制代码

增强for

复制代码
 HashMap<String, Integer> map = new HashMap<>();
        map.put("张三",168);
        map.put("李四",12);
        map.put("王五",45);
        System.out.println(map);

for (String key :map.keySet()) {
            Integer integer = map.get(key);
            System.out.println(key+integer);
        }
复制代码

 

 

 

 

Entity键值对对像:

Map.Entry<KV>:在Map接口中有一个内部接口Entry
作用:当Map集合一创建,那么就会在Map集合中创建一个Entry对象,用来记录键与值(健值对对象,键与值的映射关系)-->结婚证

Map中存放的是两种对象,一种称为key(键),一种称为value(值),它们在在Map中是一一对应关系

图:

 

 

posted on   淤泥不染  阅读(99)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

点击右上角即可分享
微信分享提示