HashMap 的 7 種遍歷方式

HashMap 遍歷

大體上可以分爲 4 類:

1, 迭代器

2,ForEach 遍歷

3,lambda 表達式遍歷

4,StreamsApi 遍歷

但是每種類型下有不同的實現方式,所以又可以分爲 7 種:

案例 demo

1,使用迭代器 EntrySet 的方式遍歷

   @Test
    //1,使用迭代器 EntrySet 的方式遍歷
    public void demo1(){
        //創建Map 對象
        Map<Integer, String> map = new HashMap<>();
        //添加數據
        map.put(1,"嬌嬌");
        map.put(2,"嬌嬌1");
        map.put(3,"嬌嬌2");
        map.put(4,"嬌嬌3");
        map.put(5,"嬌嬌4");
        map.put(5,"嬌嬌5");
    //遍歷
        Iterator<Map.Entry<Integer, String>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()){
            Map.Entry<Integer, String> next = iterator.next();
            System.out.println(next.getKey());
            System.out.println(next.getValue());
        }
    }

結果

2,使用迭代器的 KeySet

   @Test
//   2,使用迭代器的KeySet
    public void demo1(){
        //創建Map 對象
        Map<Integer, String> map = new HashMap<>();
        //添加數據
        map.put(1,"嬌嬌");
        map.put(2,"嬌嬌1");
        map.put(3,"嬌嬌2");
        map.put(4,"嬌嬌3");
        map.put(5,"嬌嬌4");
        map.put(5,"嬌嬌5");
    //遍歷
        Iterator<Integer> iterator = map.keySet().iterator();
        while (iterator.hasNext()){
            Integer key = iterator.next();
            System.out.print(key);
            System.out.print(map.get(key));
        }
    }

結果

3,使用 For Each EntrySet 的方式進行遍歷;

  @Test
//3,使用 For Each EntrySet 的方式進行遍歷;
    public void demo1(){
        //創建Map 對象
        Map<Integer, String> map = new HashMap<>();
        //添加數據
        map.put(1,"嬌嬌");
        map.put(2,"嬌嬌1");
        map.put(3,"嬌嬌2");
        map.put(4,"嬌嬌3");
        map.put(5,"嬌嬌4");
        map.put(5,"嬌嬌5");
    //遍歷
        for (Map.Entry<Integer,String> entry: map.entrySet()
             ) {
            System.out.println("entry.getKey() = " + entry.getKey());
            System.out.println("entry.getValue() = " + entry.getValue());
        }
    }

結果

4,使用 For Each KeySet 的方式進行遍歷;

  @Test
//4,使用 For Each KeySet 的方式進行遍歷;
    public void demo1(){
        //創建Map 對象
        Map<Integer, String> map = new HashMap<>();
        //添加數據
        map.put(1,"嬌嬌");
        map.put(2,"嬌嬌1");
        map.put(3,"嬌嬌2");
        map.put(4,"嬌嬌3");
        map.put(5,"嬌嬌4");
        map.put(5,"嬌嬌5");
    //遍歷
        for (Integer key: map.keySet()
             ) {
            System.out.println(key);
            System.out.println(map.get(key));
        }
    }

結果

5,使用 Lambda 表達式的方式進行遍歷;

    @Test
//5,使用 Lambda 表達式的方式進行遍歷;
    public void demo1() {
        //創建Map 對象
        Map<Integer, String> map = new HashMap<>();
        //添加數據
        map.put(1, "嬌嬌");
        map.put(2, "嬌嬌1");
        map.put(3, "嬌嬌2");
        map.put(4, "嬌嬌3");
        map.put(5, "嬌嬌4");
        map.put(5, "嬌嬌5");
        //遍歷
        map.forEach((key,value) -> {
            System.out.print(key);
            System.out.print(value);


        });

    }

結果

6,使用 Streams API 單線程的方式進行遍歷;

    @Test
//6,使用 Streams API 單線程的方式進行遍歷;
    public void demo1() {
        //創建Map 對象
        Map<Integer, String> map = new HashMap<>();
        //添加數據
        map.put(1, "嬌嬌");
        map.put(2, "嬌嬌1");
        map.put(3, "嬌嬌2");
        map.put(4, "嬌嬌3");
        map.put(5, "嬌嬌4");
        map.put(5, "嬌嬌5");
        //遍歷
        map.entrySet().stream().forEach((integerStringEntry -> {
            System.out.println(integerStringEntry.getKey());
            System.out.println(integerStringEntry.getValue());

        }));

    }

結果

7,使用 Streams API 多線程的方式進行遍歷。

    @Test
//6,使用 Streams API 單線程的方式進行遍歷;
    public void demo1() {
        //創建Map 對象
        Map<Integer, String> map = new HashMap<>();
        //添加數據
        map.put(1, "嬌嬌");
        map.put(2, "嬌嬌1");
        map.put(3, "嬌嬌2");
        map.put(4, "嬌嬌3");
        map.put(5, "嬌嬌4");
        map.put(5, "嬌嬌5");
        //遍歷
        map.entrySet().parallelStream().forEach((integerStringEntry -> {
            System.out.println(integerStringEntry.getKey());
            System.out.println(integerStringEntry.getValue());

        }));

    }

結果

————————————————

版權聲明:本文爲 CSDN 博主「我的代碼沒錯」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。

原文鏈接:

https://blog.csdn.net/m0_46937429/article/details/114999490

本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源https://mp.weixin.qq.com/s/p7FRPwqGcUMOM-Bnq2lluA