zoukankan      html  css  js  c++  java
  • Map集合遍历之第二种方法(键值对)

     “键值对“的方式遍历,更加面向对象的方式,代码复杂。

    案例:

    package com.itheima.Map复习;
    
    import java.util.*;
    
    /**
     * @program: javaDemo01->MapTestDemo01
     * @description: Map遍历复习
     * @author: 安生
     * @create: 2021-02-02 17:25
     **/
    public class MapTestDemo01 {
        private static final Map<String,Integer> ALL_PROPLE = new HashMap<>();
    
        static {
           ALL_PROPLE.put("陈平安",16);
           ALL_PROPLE.put("阿良",18);
           ALL_PROPLE.put("齐静春",16);
    //        System.out.println(ALL_PROPLE);
        }
        public static void main(String[] args) {
            /**
             * 第二种遍历方式 "键值对"
             */
            Set<Map.Entry<String, Integer>> entries = ALL_PROPLE.entrySet();
            for (Map.Entry<String, Integer> entry : entries) {
                String key = entry.getKey();
                Integer value = entry.getValue();
                System.out.println(key + "=>" + value);
            }
    
            //快捷方式 直接 ALL_PEOPLE.entrySet.for
            for (Map.Entry<String, Integer> stringIntegerEntry : ALL_PROPLE.entrySet()) {
                String key = stringIntegerEntry.getKey();
                Integer value = stringIntegerEntry.getValue();
                System.out.println(key + "=>" + value);
            }
    
    
        }
    }

    运行结果:

  • 相关阅读:
    矩阵快速幂
    快速排序
    用闭包来实现命令模式
    闭包和面向对象设计
    闭包及其作用
    阿里笔试
    如何在节点上添加样式
    getComputedStyle与currentStyle获取样式(style/class)
    今日头条笔试
    牛客网JavaScript编程规范
  • 原文地址:https://www.cnblogs.com/bichen-01/p/14363602.html
Copyright © 2011-2022 走看看