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);
            }
    
    
        }
    }

    运行结果:

  • 相关阅读:
    UVA 1386 Cellular Automaton
    ZOJ 3331 Process the Tasks
    CodeForces 650B Image Preview
    CodeForces 650A Watchmen
    CodeForces 651B Beautiful Paintings
    CodeForces 651A Joysticks
    HUST 1601 Shepherd
    HUST 1602 Substring
    HUST 1600 Lucky Numbers
    POJ 3991 Seinfeld
  • 原文地址:https://www.cnblogs.com/bichen-01/p/14363602.html
Copyright © 2011-2022 走看看