zoukankan      html  css  js  c++  java
  • 遍历map的几种方法

    遍历map有几种方法,记录一下。

    public class MapCollection {
    public static void main(String[] args) {
    HashMap<Integer, String> map = new HashMap<>();
    map.put(1,"小花");
    map.put(2,"小草");
    map.put(3,"小红");
    map.put(4,"小白");

    System.out.println("-----通过Map.keySet()获取key,根据key调用map.get()获取值value------");
    for (Integer key:map.keySet()) {
    System.out.println(key+":"+map.get(key));
    }

    System.out.println("-----通过Map.entrySet()获取迭代器对象iterator,遍历key和value-----");
    Iterator<Map.Entry<Integer, String>> it = map.entrySet().iterator();
    while(it.hasNext()){
    Map.Entry<Integer, String> entry = it.next();
    System.out.println(entry.getKey()+":"+entry.getValue());
    }

    System.out.println("-----通过Map.entrySet()遍历key和value-------");
    for (Map.Entry<Integer,String> entry:map.entrySet()) {
    System.out.println(entry.getKey()+":"+entry.getValue());
    }

    System.out.println("-----通过Map.values()遍历所有的value-----");
    System.out.println("-----此方法无法获key----------");
    for (String value: map.values()) {
    System.out.println(value);
    }
    }
    }
  • 相关阅读:
    MongoDB简单使用
    mongodb安装部署
    分布式通信-序列化
    分布式通信协议
    分布式概念
    springboot-事件
    spring-事件
    spring-@Component/@ComponentScan注解
    springboot-Date日期时间问题
    enginx:基于openresty,一个前后端统一,生态共享的webstack实现
  • 原文地址:https://www.cnblogs.com/lifengSkt/p/13233482.html
Copyright © 2011-2022 走看看