Map集合的遍历方式之第一种(键找值)(重点)

 目标: Map集合的遍历方式。

    Map集合的遍历方式有三种:

      a.“键找值”的方式遍历:先获取Map集合全部的键,再根据遍历键找值。

      b.“键值对”的方式遍历;难度较大。

      c.JDK 1.8开始之后的新技术,Lambda表达式。(暂时了解)

    1.“键找值”的方式遍历:

      a.先获取Map集合的全部键的Set集合、

      b.遍历键的Set集合,然后通过键找值。

小结: 代码简单 需要记住!

案例:

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) {
        List<Integer> ages = new ArrayList<>();
        Set<String> names = ALL_PROPLE.keySet();
        for (String name : names) {
            Collections.addAll(ages,ALL_PROPLE.get(name));

        }
        for (Integer age : ages) {
            System.out.println(age);
        }
    }
}

运行结果:

原文地址:https://www.cnblogs.com/bichen-01/p/14363282.html