zoukankan      html  css  js  c++  java
  • Map的3种遍历[轉]

    Map<String, String> map = new HashMap<String, String>();
    map.put("A", "AAA");
    map.put("B", "BBB");
    map.put("C", "CCC");
    map.put("D", "DDD");

    // 第一种用for循环

    for (Map.Entry<String, String> entry : map.entrySet()) {
    System.out.println(entry.getKey() + "--->" + entry.getValue());
    }

    // 第二种用迭代

    Set set = map.entrySet();
    Iterator it = set.iterator();
    while (it.hasNext()) {
    Map.Entry<String, String> entry1 = (Map.Entry<String, String>) it.next();
    System.out.println(entry1.getKey() + "==" + entry1.getValue());
    }

    Iterator it = map.keySet().iterator();
    while (it.hasNext()) {
    String key;
    String value;
    key = it.next().toString();
    value = map.get(key);
    System.out.println(key + "--" + value);
    }

    // 用entrySet()迭代

    Iterator<Entry<String, String>> it = map.entrySet().iterator();
    System.out.println(map.entrySet().size());
    String key;
    String value;
    while (it.hasNext()) {
    Map.Entry entry = it.next();
    key = entry.getKey().toString();
    value = entry.getValue().toString();
    System.out.println(key + "====" + value);
    }

  • 相关阅读:
    配置禅道遇到的那些事儿
    HDU 1181
    HDU1016
    HDU 1518
    2015长春区域赛赛后总结
    Codeforces Round #322 (Div. 2) A B C
    Codeforces Round #325 (Div. 2) A B
    Codeforces Round #324 (Div. 2) A B
    CSU 1530
    CSU 1535
  • 原文地址:https://www.cnblogs.com/taojintianxia/p/3312651.html
Copyright © 2011-2022 走看看