Java中遍历Map集合的五种方式
生活随笔
收集整理的這篇文章主要介紹了
Java中遍历Map集合的五种方式
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
寫在前面: 我是「揚帆向海」,這個昵稱來源于我的名字以及女朋友的名字。我熱愛技術、熱愛開源、熱愛編程。技術是開源的、知識是共享的。
這博客是對自己學習的一點點總結及記錄,如果您對 Java、算法 感興趣,可以關注我的動態,我們一起學習。
用知識改變命運,讓我們的家人過上更好的生活。
文章目錄
- 方式一 通過Map.keySet使用iterator遍歷
- 方式二 通過Map.entrySet使用iterator遍歷
- 方式三 通過Map.keySet遍歷
- 方式四 通過For-Each迭代entries,使用Map.entrySet遍歷
- 方式五 使用lambda表達式forEach遍歷
- 總結
方式一 通過Map.keySet使用iterator遍歷
@Test public void testHashMap1() {Map<Integer, String> map = new HashMap<>();map.put(001, "Java");map.put(002, "數據庫");map.put(003, "Vue");System.out.println(map);// 通過Map.keySet使用iterator遍歷key,然后通過key得到對應的value值Iterator<Integer> iterator = map.keySet().iterator();while (iterator.hasNext()) {Integer key = iterator.next();String value = map.get(key);System.out.println("key = " + key + ", value = " + value);} }結果:
{1=Java, 2=數據庫, 3=Vue} key = 1, value = Java key = 2, value = 數據庫 key = 3, value = Vue方式二 通過Map.entrySet使用iterator遍歷
@Test public void testHashMap2() {Map<Integer, String> map = new HashMap<>();map.put(001, "Java");map.put(002, "數據庫");map.put(003, "Vue");System.out.println(map);// 通過Map.entrySet使用iterator遍歷key和value;注意 Set entrySet():返回所有key-value對構成的Set集合Iterator<Map.Entry<Integer, String>> entries = map.entrySet().iterator();while (entries.hasNext()) {Map.Entry<Integer, String> entry = entries.next();System.out.println(entry);} }結果:
{1=Java, 2=數據庫, 3=Vue} 1=Java 2=數據庫 3=Vue方式三 通過Map.keySet遍歷
@Test public void testHashMap3() {Map<Integer, String> map = new HashMap<>();map.put(001, "Java");map.put(002, "數據庫");map.put(003, "Vue");System.out.println(map);// 通過Map.keySet遍歷key,然后通過key得到對應的value值for (Integer key : map.keySet()) {System.out.println("key = " + key + ", value = " + map.get(key));} }結果:
{1=Java, 2=數據庫, 3=Vue} key = 1, value = Java key = 2, value = 數據庫 key = 3, value = Vue方式四 通過For-Each迭代entries,使用Map.entrySet遍歷
@Test public void testHashMap4() {Map<Integer, String> map = new HashMap<>();map.put(001, "Java");map.put(002, "數據庫");map.put(003, "Vue");System.out.println(map);// 使用For-Each迭代entries,通過Map.entrySet遍歷key和valuefor (Map.Entry<Integer, String> entry : map.entrySet()) {System.out.println("key = " + entry.getKey() + ", value = " + entry.getValue());} } {1=Java, 2=數據庫, 3=Vue} key = 1, value = Java key = 2, value = 數據庫 key = 3, value = Vue方式五 使用lambda表達式forEach遍歷
@Test public void testHashMap5() {Map<Integer, String> map = new HashMap<>();map.put(001, "Java");map.put(002, "數據庫");map.put(003, "Vue");System.out.println(map);// 使用lambda表達式forEach遍歷map.forEach((k, v) -> System.out.println("key = " + k + ", value = " + v)); }forEach 源碼
default void forEach(BiConsumer<? super K, ? super V> action) {Objects.requireNonNull(action);for (Map.Entry<K, V> entry : entrySet()) {K k;V v;try {k = entry.getKey();v = entry.getValue();} catch(IllegalStateException ise) {// this usually means the entry is no longer in the map.throw new ConcurrentModificationException(ise);}action.accept(k, v);}}從源碼可以看到,這種新特性就是在傳統的迭代方式上加了一層殼,但是讓代碼變得更加簡單。(開發中推薦使用)
總結
推薦使用 entrySet 遍歷 Map 類集合 KV (文章中的第四種方式),而不是 keySet 方式進行遍歷。
keySet 其實是遍歷了 2 次,第一次是轉為 Iterator 對象,第二次是從 hashMap 中取出 key 所對應的 value值。而 entrySet 只是遍歷了一次,就把 key 和 value 都放到了 entry 中,效率更高。
values()返回的是 V 值集合,是一個 list 集合對象;keySet()返回的是 K 值集合,是一個 Set 集合對象;entrySet()返回的是 K-V 值組合集合。
如果是 JDK8,推薦使用Map.forEach 方法(文章中的第五種方式)。
由于水平有限,本博客難免有不足,懇請各位大佬不吝賜教!
總結
以上是生活随笔為你收集整理的Java中遍历Map集合的五种方式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C 结构体
- 下一篇: epoll模型之服务器设计