<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          HashMap 的7種遍歷方式

          共 9807字,需瀏覽 20分鐘

           ·

          2021-03-26 10:43

          點擊上方藍色字體,選擇“標(biāo)星公眾號”

          優(yōu)質(zhì)文章,第一時間送達

          76套java從入門到精通實戰(zhàn)課程分享

          HashMap 遍歷

          大體上可以分為4類:

          1,迭代器

          2,F(xiàn)orEach 遍歷

          3,lambda 表達式遍歷

          4,StreamsApi 遍歷


          但是每種類型下有不同的實現(xiàn)方式,所以又可以分為7種:

          案例demo

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

             @Test
              //1,使用迭代器 EntrySet 的方式遍歷
              public void demo1(){
                  //創(chuàng)建Map 對象
                  Map<Integer, String> map = new HashMap<>();
                  //添加數(shù)據(jù)
                  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());
                  }
              }


          結(jié)果


          2,使用迭代器的KeySet

             @Test
          //   2,使用迭代器的KeySet
              public void demo1(){
                  //創(chuàng)建Map 對象
                  Map<Integer, String> map = new HashMap<>();
                  //添加數(shù)據(jù)
                  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));
                  }
              }


          結(jié)果


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

            @Test
          //3,使用 For Each EntrySet 的方式進行遍歷;
              public void demo1(){
                  //創(chuàng)建Map 對象
                  Map<Integer, String> map = new HashMap<>();
                  //添加數(shù)據(jù)
                  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());
                  }
              }


          結(jié)果


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

            @Test
          //4,使用 For Each KeySet 的方式進行遍歷;
              public void demo1(){
                  //創(chuàng)建Map 對象
                  Map<Integer, String> map = new HashMap<>();
                  //添加數(shù)據(jù)
                  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));
                  }
              }


          結(jié)果


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

              @Test
          //5,使用 Lambda 表達式的方式進行遍歷;
              public void demo1() {
                  //創(chuàng)建Map 對象
                  Map<Integer, String> map = new HashMap<>();
                  //添加數(shù)據(jù)
                  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);


                  });

              }


          結(jié)果


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

              @Test
          //6,使用 Streams API 單線程的方式進行遍歷;
              public void demo1() {
                  //創(chuàng)建Map 對象
                  Map<Integer, String> map = new HashMap<>();
                  //添加數(shù)據(jù)
                  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());

                  }));

              }


          結(jié)果


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

              @Test
          //6,使用 Streams API 單線程的方式進行遍歷;
              public void demo1() {
                  //創(chuàng)建Map 對象
                  Map<Integer, String> map = new HashMap<>();
                  //添加數(shù)據(jù)
                  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());

                  }));

              }


          結(jié)果

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

          版權(quán)聲明:本文為CSDN博主「我的代碼沒錯」的原創(chuàng)文章,遵循CC 4.0 BY-SA版權(quán)協(xié)議,轉(zhuǎn)載請附上原文出處鏈接及本聲明。

          原文鏈接:

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





          粉絲福利:Java從入門到入土學(xué)習(xí)路線圖

          ??????

          ??長按上方微信二維碼 2 秒


          感謝點贊支持下哈 

          瀏覽 31
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  免费又色又爽又黄的成人用品 | 免费看黄色电影 | 亚洲人成色777777无码 | 欧美成人色图专区 | 久久鬼色 |