zoukankan      html  css  js  c++  java
  • 遍历Map的常见四种方法

     1 package com.test;
     2 
     3 import java.util.HashMap;
     4 import java.util.Iterator;
     5 import java.util.Map;
     6 import java.util.Set;
     7 
     8 /**
     9  * Created by Donge on 2017/2/6.
    10  */
    11 public class TestMap {
    12     public static void main(String[] args) {
    13         Map<Integer, String> map = new HashMap<>();
    14         map.put(1, "v1");
    15         map.put(2, "v2");
    16         map.put(3, "v3");
    17         // 方法一 推荐
    18         for (Map.Entry e : map.entrySet()) {
    19             System.out.println(e.getKey() + " : " + e.getValue());
    20         }
    21         System.out.println("*************************");
    22         // 方法二
    23         Set<Integer> keys = map.keySet();
    24         for (Integer k : keys){
    25             System.out.println(k+" : "+map.get(k));
    26         }
    27         System.out.println("*************************");
    28         // 方法三
    29         Iterator<Integer> iterator = map.keySet().iterator();
    30         while (iterator.hasNext()){
    31             Integer key = iterator.next();
    32             System.out.println(key+" : "+map.get(key));
    33         }
    34         System.out.println("*************************");
    35         // 方法四 此方法不能获取key值
    36         for (String v : map.values()){
    37             System.out.println(v);
    38         }
    39     }
    40 }
  • 相关阅读:
    多传感器融合(三)
    多传感器融合(二)
    多传感器融合(一)
    3D点云完美匹配
    Geo-CNN的三维点云
    3D点云几何拟合
    BAD SLAM:捆绑束调整直接RGB-D SLAM
    三维视觉惯性SLAM的有效Schmidt-EKF
    RGB-D相机视觉SLAM
    Visual SLAM
  • 原文地址:https://www.cnblogs.com/liqingdong/p/6378408.html
Copyright © 2011-2022 走看看