ハッシュマップを繰り返す方法は何がありますか?

HashMapをトラバースする方法は以下のようにいくつかあります:

  1. エントリーセット()
HashMap<String, Integer> map = new HashMap<>();
// 添加键值对
Iterator<Map.Entry<String, Integer>> iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
    Map.Entry<String, Integer> entry = iterator.next();
    String key = entry.getKey();
    Integer value = entry.getValue();
    // 处理键值对
}
  1. キーのセット
HashMap<String, Integer> map = new HashMap<>();
// 添加键值对
for (String key : map.keySet()) {
    Integer value = map.get(key);
    // 处理键值对
}
  1. バリュー()
HashMap<String, Integer> map = new HashMap<>();
// 添加键值对
for (Integer value : map.values()) {
    // 处理值
}
  1. Java 8を使用してforEachを使うと、HashMap内のキーと値のペアを簡単に走査できます。
HashMap<String, Integer> map = new HashMap<>();
// 添加键值对
map.forEach((key, value) -> {
    // 处理键值对
});
bannerAds