• Java中Map的 entrySet() 详解以及用法(四种遍历map的方式)


    Entry
    由于Map中存放的元素均为键值对,故每一个键值对必然存在一个映射关系。
    Map中采用Entry内部类来表示一个映射项,映射项包含Key和Value (我们总说键值对键值对, 每一个键值对也就是一个Entry)
    Map.Entry里面包含getKey()和getValue()方法

    Iterator> it=map.entrySet().iterator();
        while(it.hasNext()) {
            Map.Entry entry=it.next();
            int key=entry.getKey();
            int value=entry.getValue();
            System.out.println(key+" "+value);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    entrySet

    entrySet是java中 键-值对的集合,Set里面的类型是Map.Entry,一般可以通过map.entrySet()得到。

    • entrySet实现了Set接口,里面存放的是键值对。一个K对应一个V。

    用来遍历map的一种方法。

    Set> entryseSet=map.entrySet();
     
    for (Map.Entry entry:entryseSet) {
     
        System.out.println(entry.getKey()+","+entry.getValue());
     
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    即通过getKey()得到K,getValue得到V。

    keySet

    还有一种是keySet, keySet是键的集合,Set里面的类型即key的类型

    Set set = map.keySet();
     
    for (String s:set) {
     
        System.out.println(s+","+map.get(s));
     
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    四种遍历Map方式:

    public static void main(String[] args) {
     
        Map map = new HashMap();
        map.put("1", "value1");
        map.put("2", "value2");
        map.put("3", "value3");
      
        //第一种:普遍使用,二次取值
        System.out.println("通过Map.keySet遍历key和value:");
        for (String key : map.keySet()) {
            System.out.println("key= "+ key + " and value= " + map.get(key));
        }
      
        //第二种
        System.out.println("通过Map.entrySet使用iterator遍历key和value:");
        Iterator> it = map.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry entry = it.next();
            System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
        }
      
        //第三种:推荐,尤其是容量大时
        System.out.println("通过Map.entrySet遍历key和value");
        for (Map.Entry entry : map.entrySet()) {
            System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
        }
     
        //第四种
        System.out.println("通过Map.values()遍历所有的value,但不能遍历key");
        for (String v : map.values()) {
            System.out.println("value= " + v);
        }
     }
    
    • 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

    ————————————————
    版权声明:本文为CSDN博主「NO0b」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/q5706503/article/details/85122343

  • 相关阅读:
    软考学习笔记
    linux基础IO
    APK 逆向工程 - 解析 apk 基本信息和方法调用图
    Vue3 相较 Vue2 做的重大更新
    十个强大的 React 工具
    Rust可空类型Option
    插件化踩坑之路——Small和Atlas方案对比
    bug的生命周期都有那些阶段
    Python 的四舍五入的两个方法,你学会了吗?
    AQS详解
  • 原文地址:https://blog.csdn.net/m0_67402774/article/details/126726772