日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java中遍历HashMap的5种方式

發(fā)布時間:2023/12/9 java 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java中遍历HashMap的5种方式 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

From: https://blog.csdn.net/w605283073/article/details/80708943

本教程將為你展示Java中HashMap的幾種典型遍歷方式。

如果你使用Java8,由于該版本JDK支持lambda表達式,可以采用第5種方式來遍歷。

如果你想使用泛型,可以參考方法3。如果你使用舊版JDK不支持泛型可以參考方法4。

1、 通過ForEach循環(huán)進行遍歷

  • mport java.io.IOException;

  • import java.util.HashMap;

  • import java.util.Map;

  • ?
  • public class Test {

  • public static void main(String[] args) throws IOException {

  • Map<Integer, Integer> map = new HashMap<Integer, Integer>();

  • map.put(1, 10);

  • map.put(2, 20);

  • ?
  • // Iterating entries using a For Each loop

  • for (Map.Entry<Integer, Integer> entry : map.entrySet()) {

  • System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());

  • }

  • ?
  • }

  • }

  • 2、 ForEach迭代鍵值對方式

    如果你只想使用鍵或者值,推薦使用如下方式

  • import java.io.IOException;

  • import java.util.HashMap;

  • import java.util.Map;

  • ?
  • public class Test {

  • public static void main(String[] args) throws IOException {

  • Map<Integer, Integer> map = new HashMap<Integer, Integer>();

  • map.put(1, 10);

  • map.put(2, 20);

  • ?
  • // 迭代鍵

  • for (Integer key : map.keySet()) {

  • System.out.println("Key = " + key);

  • }

  • ?
  • // 迭代值

  • for (Integer value : map.values()) {

  • System.out.println("Value = " + value);

  • }

  • }

  • }

  • 3、使用帶泛型的迭代器進行遍歷

  • import java.io.IOException;

  • import java.util.HashMap;

  • import java.util.Iterator;

  • import java.util.Map;

  • ?
  • public class Test {

  • public static void main(String[] args) throws IOException {

  • Map<Integer, Integer> map = new HashMap<Integer, Integer>();

  • map.put(1, 10);

  • map.put(2, 20);

  • ?
  • Iterator<Map.Entry<Integer, Integer>> entries = map.entrySet().iterator();

  • while (entries.hasNext()) {

  • Map.Entry<Integer, Integer> entry = entries.next();

  • System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());

  • }

  • }

  • }

  • 4、使用不帶泛型的迭代器進行遍歷

  • import java.io.IOException;

  • import java.util.HashMap;

  • import java.util.Iterator;

  • import java.util.Map;

  • ?
  • public class Test {

  • ?
  • public static void main(String[] args) throws IOException {

  • ?
  • Map map = new HashMap();

  • map.put(1, 10);

  • map.put(2, 20);

  • ?
  • Iterator<Map.Entry> entries = map.entrySet().iterator();

  • while (entries.hasNext()) {

  • Map.Entry entry = (Map.Entry) entries.next();

  • Integer key = (Integer) entry.getKey();

  • Integer value = (Integer) entry.getValue();

  • System.out.println("Key = " + key + ", Value = " + value);

  • }

  • }

  • }

  • 5、通過Java8 Lambda表達式遍歷

  • import java.io.IOException;

  • import java.util.HashMap;

  • import java.util.Map;

  • ?
  • public class Test {

  • ?
  • public static void main(String[] args) throws IOException {

  • ?
  • Map<Integer, Integer> map = new HashMap<Integer, Integer>();

  • map.put(1, 10);

  • map.put(2, 20);

  • map.forEach((k, v) -> System.out.println("key: " + k + " value:" + v));

  • }

  • }

  • ?

    輸出

    key: 1 value:10 key: 2 value:20

    總結

    以上是生活随笔為你收集整理的Java中遍历HashMap的5种方式的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。