Java - Map集合的遍歷

目錄

第一種方式:通過鍵找值的方式

第二種方式:使用Entry對象遍歷


 

Java Map集合的遍歷大致是是有兩種方式:

第一種方式:通過鍵找值的方式

 Map集合的第一種遍歷方式:通過鍵找值的方式:
    Map集合中得方法:
        Set<K> keySet():返回此映射中包含的鍵的Set視圖。
    實現步驟:
        1.使用Map集合中的方法keySet(),把Map集合中所有的key取出來,存儲到一個Set集合中
        2.遍歷Set集合,獲取Map集合中的每一個key
        3.通過Map集合中的方法get(key),通過key找到value

實例:

import java.util.*;
/*
    Map集合的第一種遍歷方式:通過鍵找值得方式:
    Map集合中得方法:
        Set<K> keySet():返回此映射中包含的鍵的Set視圖。
    實現步驟:
        1.使用Map集合中的方法keySet(),把Map集合中所有的key取出來,存儲到一個Set集合中
        2.遍歷Set集合,獲取Map集合中的每一個key
        3.通過Map集合中的方法get(key),通過key找到value
 */

public class Main {
    public static void main(String[] args) {
        //Scanner cin = new Scanner(System.in);
        show04();
    }

    public static void show04(){
        //創建Map集合對象
        Map<String, Integer> map = new HashMap<>();
        map.put("趙麗穎", 168);
        map.put("楊穎", 165);
        map.put("林志玲", 178);

        // 1.使用Map集合中的方法keySet(),把Map集合中所有的key取出來,存儲到一個Set集合中
        Set<String> set = map.keySet();
        //2.遍歷Set集合,獲取Map集合中的每一個key
        //使用迭代器遍歷Set集合
        Iterator<String> it = set.iterator();
        while(it.hasNext()){
            String key = it.next();
            //3.通過Map集合中的方法get(key),通過key找到value
            Integer value = map.get(key);
            System.out.println(key + " = " + value);
        }

        System.out.println("------------------------------");
        //使用增強for遍歷Set集合
        for (String key : map.keySet()){
            //3.通過Map集合中的方法get(key),通過key找到value
            Integer value = map.get(key);
            System.out.println(key + " = " + value);
        }
        /*運行結果
        林志玲 = 178
        趙麗穎 = 168
        楊穎 = 165
        ------------------------------
        林志玲 = 178
        趙麗穎 = 168
        楊穎 = 165
         */
    }
}

第二種方式:使用Entry對象遍歷

預備知識:Java-Map 中的Entry對象:

Map集合的第二種遍歷方式:使用Entry對象遍歷
    Map集合中得方法:
        Set<Map.Entry<K, V>> entrySet() 返回此映射中包含的映射關係的Set視圖
    實現步驟:
        1.使用Map集合中的方法entrySet(),把Map集合中多個Entry對象取出來,存儲到一個Set集合中
        2.遍歷Set集合,獲取每一個Entry對象
        3.通過Entry對象中的方法getKey()和getValue()獲取鍵與值

實例:


import java.util.*;
/*
    Map集合的第二種遍歷方式:使用Entry對象遍歷
    Map集合中得方法:
        Set<Map.Entry<K, V>> entrySet() 返回此映射中包含的映射關係的Set視圖
    實現步驟:
        1.使用Map集合中的方法entrySet(),把Map集合中多個Entry對象取出來,存儲到一個Set集合中
        2.遍歷Set集合,獲取每一個Entry對象
        3.通過Entry對象中的方法getKey()和getValue()獲取鍵與值
 */

public class Main {
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        //創建Map集合對象
        Map<String, Integer> map = new HashMap<>();
        map.put("趙麗穎", 168);
        map.put("楊穎", 165);
        map.put("林志玲", 178);

        //1.使用Map集合中的方法entrySet(),把Map集合中多個Entry對象取出來,存儲到一個Set集合中
        Set<Map.Entry<String, Integer>> set = map.entrySet();

        //2.遍歷Set集合,獲取每一個Entry對象
        //使用迭代器遍歷Set集合
        Iterator<Map.Entry<String, Integer>> it = set.iterator();
        while (it.hasNext()){
            Map.Entry<String, Integer> entry = it.next();
            //3.使用Entry對象中的方法getKey()和getValue()獲取鍵與值
            String key = entry.getKey();
            Integer value = entry.getValue();
            System.out.println(key + " = " + value);
        }
        System.out.println("---------------------");
        for (Map.Entry<String, Integer> entry : set){
            String key = entry.getKey();
            Integer value = entry.getValue();
            System.out.println(key + " = " + value);
        }
        /*運行結果
        林志玲 = 178
        趙麗穎 = 168
        楊穎 = 165
        ---------------------
        林志玲 = 178
        趙麗穎 = 168
        楊穎 = 165
         */
    }
}

 

發佈了424 篇原創文章 · 獲贊 102 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章