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

    创建一个MAP的栗子:

    1         Map<String, Integer> tempMap = new HashMap<String, Integer>();
    2         tempMap.put("abc", 1);
    3         tempMap.put("aoe", 2);
    4         tempMap.put("xyz", 3);

    方法一:

    1         Iterator<Entry<String, Integer>> it = tempMap.entrySet().iterator();
    2         while (it.hasNext()) {
    3             Entry<String, Integer> entry = it.next();
    4             Object key = entry.getKey();
    5             Object value = entry.getValue();
    6             System.out.println("key=" + key + " value=" + value);
    7         }

    方法二:

    1         for (Map.Entry<String, Integer> entry : tempMap.entrySet()) {
    2             String key = entry.getKey().toString();
    3             String value = entry.getValue().toString();
    4             System.out.println("key=" + key + " value=" + value);
    5         }

    方法三:

    1         for (Iterator<String> i = tempMap.keySet().iterator(); i.hasNext();) {
    2             Object obj = i.next();
    3             System.out.println(obj);
    4             System.out.println("key=" + obj + " value=" + tempMap.get(obj));
    5         }
    6         for (Iterator<Integer> i = tempMap.values().iterator(); i.hasNext();) {
    7             Object obj = i.next();
    8             System.out.println(obj);
    9         }

    方法四:

    1         for (Object o : tempMap.keySet()) {
    2             System.out.println("key=" + o + " value=" + tempMap.get(o));
    3         }
  • 相关阅读:
    [LeetCode]Subsets II
    [LeetCode]Subsets
    [LeetCode]Combinations
    [LeetCode]Minimum Window Substring
    [LeetCode]Search a 2D Matrix
    [LeetCode]Edit Distance
    [LeetCode]Simplify Path
    Adaboost算法
    [LeetCode]Text Justification
    31、剑指offer--从1到n整数中1出现次数
  • 原文地址:https://www.cnblogs.com/SummerinShire/p/5390696.html
Copyright © 2011-2022 走看看