zoukankan      html  css  js  c++  java
  • 遍历Map集合的几种方法

    遍历Map集合的几种方法


    方法1:使用迭代器iterator遍历集合

    HashMap<Integer, Long> map = new HashMap<Integer, Long>();
      for (int i = 1; i <= 50; i++) {
      map.put(i, Math.round(3.14*i*i));
    }
    
    // map转换为set集合
    Set<Entry<Integer, Long>> set = map.entrySet();
    
    // 使用迭代器Iterator遍历set集合 
    Iterator
    <Entry<Integer, Long>> it = set.iterator();   
    while (it.hasNext()) {   
      Entry
    <Integer, Long> next = it.next();   
      Integer key
    = next.getKey();   
      Long value
    = next.getValue();   
      System.out.println(key
    +":"+value);
    }

    方法2:使用增强for循环遍历集合

    
    
    HashMap<Integer, Long> map = new HashMap<Integer, Long>();
      for (int i = 1; i <= 50; i++) {
      map.put(i, Math.round(3.14*i*i));
    }
    
    // map转换为set集合
    Set<Entry<Integer, Long>> set = map.entrySet();
    for (Entry<Integer, Long> entry : set) {
      Integer key = entry.getKey();
      Long value = entry.getValue(); 
      System.out.println(key+":"+value);
    }
  • 相关阅读:
    Linux上安装软件
    VIM的使用
    Linux命令(系统管理)
    django潜心之路4--模型
    django潜行之路3-模板
    django潜行之路2---视图
    django潜行之路1
    sql基础
    Mysql数据库总结
    反爬虫机制
  • 原文地址:https://www.cnblogs.com/snow1234/p/7195824.html
Copyright © 2011-2022 走看看