HashMap的7种遍历方式
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                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(6, "嬌嬌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());})); }結果
————————————————
總結
以上是生活随笔為你收集整理的HashMap的7种遍历方式的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: 如何使用Dirsearch探测Web目录
 - 下一篇: class加载过程