Map集合

Map集合介紹:

前面學的Collection是一次只能存一個的(單身),Map是有對應關係的(鍵和值)

Map集合特點:

  1. 存儲鍵值對(一個鍵和一個值)
  2. 鍵不能重複

Map常用方法

  • public V put(K key, V value) : 把指定的鍵與指定的值添加到Map集合中。
  • public V remove(Object key) : 把指定的鍵 所對應的鍵值對元素 在Map集合中刪除,返回被刪除元素的值。
  • public V get(Object key) 根據指定的鍵,在Map集合中獲取對應的值。
  • public Set keySet() : 獲取Map集合中所有的鍵,存儲到Set集合中。
  • public Set<Map.Entry<K,V>> entrySet() : 獲取到Map集合中所有的鍵值對對象的集合(Set集合)。
  • public boolean containKey(Object key) :判斷該集合中是否有此鍵。

代碼演示:

    public static void main(String[] args) {
        //創建 map對象
        HashMap<String, String> map = new HashMap<>();

        // V put​(K key, V value) 鍵不存在時添加一個鍵和一個值,鍵存在是修改值
        map.put("賈乃亮", "李小璐");
        map.put("王寶強", "馬蓉");
        map.put("陳羽凡", "白百合");
        map.put("謝霆鋒", "張柏芝");
        map.put("謝霆鋒", "王菲");

        System.out.println("map = " + map);
//       map = {賈乃亮=李小璐, 王寶強=馬蓉, 謝霆鋒=王菲, 陳羽凡=白百合}
        // V get​(Object key) 通過鍵找值
        String c = map.get("陳羽凡");
        System.out.println("c = " + c);
        String a = map.get("奧特曼");
        System.out.println("a = " + a); // 鍵不存在返回null

        // V remove​(Object key) 通過鍵刪除這對數據
        String v3 = map.remove("王寶強");
        System.out.println("remove 返回值: " + v3); // 返回被刪除的值
        System.out.println("刪除後: " + map);

        // int size​() 返回鍵值映射的數量
        int size = map.size();
        System.out.println("size = " + size);
    }

Map遍歷

什麼是map遍歷

取出map中的每鍵和對應的值

方式1:鍵找值方式

Set keySet(); 獲取Map中所有的鍵 ---------通過元素中的鍵,獲取鍵所對應的值

map遍歷鍵找值方式的步驟:

1.獲取所有的鍵
2.遍歷獲取每個鍵
3.通過鍵獲取值

代碼演示:

 public static void main(String[] args) {
        Map<String, String> map = new HashMap<>();
        map.put("賈乃亮", "李小璐");
        map.put("王寶強", "馬蓉");
        map.put("陳羽凡", "白百合");
        map.put("謝霆鋒", "張柏芝");
 		// 1.獲取所有的鍵
        Set<String> keySet = map.keySet();
        // 2.遍歷獲取每個鍵
        for (String key : keySet) {
        // 3.通過鍵獲取值
            String value = map.get(key);
            System.out.println(key + " == " + value);
        }

方式2:Map遍歷Entry方式

  • 首先了解什麼是Entry:
  • Map中存放兩種對象,key(鍵),value(值),它們在在 Map 中是一一對應關係,這一對對象又稱做 Map 中的一個 Entry(項) 。 Entry 將鍵值對的對應關係封裝成了對象。即鍵值對對象,這樣我們在遍歷 Map 集合時,就可以從每一個鍵值對( Entry )對象中獲取對應的鍵與對應的值。
  • Entry是一個接口,這個接口中會保存鍵和值.也稱爲鍵值對對象.相當於結婚證

怎麼獲取Entry呢?

Map中沒有獲取一個Entry的方法,只能獲取所有的Entry
Map中的方法:Set<Entry<K, V>>entrySet(); 獲取所有的Entry

獲取鍵和獲取值的方法:

  • public K getKey() :獲取Entry對象中的鍵。
  • public V getValue() :獲取Entry對象中的值。

遍歷的步驟:

1.獲取所有的Entry
2.遍歷集合得到每個Entry
3.通過Entry得到鍵和值

代碼演示:

   public static void main(String[] args) {
        Map<String, String> map = new HashMap<>();
        map.put("賈乃亮", "李小璐");
        map.put("王寶強", "馬蓉");
        map.put("陳羽凡", "白百合");
        map.put("謝霆鋒", "張柏芝");

        // 這種效率高一點,Map在存儲的時候就是以Entry存儲的
        // 1.獲取所有的Entry
        Set<Map.Entry<String, String>> entrySet = map.entrySet();
        // 2.遍歷集合得到每個Entry
        for (Map.Entry<String, String> entry : entrySet) {
            // 3.通過Entry得到鍵和值
            String key = entry.getKey();
            String value = entry.getValue();
            System.out.println(key + " == " + value);
        }
    }

HashMap存儲自定義類型

說明:

每位學生(姓名,年齡)都有自己的家庭住址。那麼,既然有對應關係,則將學生對象和家庭住址存儲到map集合中。學生作爲鍵, 家庭住址作爲值。注意,學生姓名相同並且年齡相同視爲同一名學生。
步驟:

1.定義學生類
2.創建一個HashMap
3.將多個學生和家庭地址存放到Map中
4.遍歷Map

代碼演示:

定義學生類:

public class Student {
    private String name;
    private int age;
    // 省略重寫hashCode(),構造方法getset和toString

測試類:

 public static void main(String[] args) {
        // 2.創建一個HashMap
        HashMap<Student, String> map = new HashMap<>();

        // 3.將多個學生和家庭地址存放到Map中
        Student s1 = new Student("流川楓", 17);
        Student s2 = new Student("櫻木花道", 16);
        Student s3 = new Student("赤木剛憲", 18);
        Student s4 = new Student("赤木晴子", 15);
        Student s5 = new Student("赤木晴子", 15);
        map.put(s1, "東京");
        map.put(s2, "大阪");
        map.put(s3, "北海道");
        map.put(s4, "長崎");
        map.put(s5, "廣島");
        // 我們將數據存儲到HashMap.HashMap底層是哈希表,
        // 哈希表是通過元素的hashCode和equals來確定是否重複

        // 4.遍歷Map
        // 鍵找值方式
        // 1.獲取所有的鍵
        Set<Student> keySet = map.keySet();
        // 2.遍歷獲取每個鍵
        for (Student key : keySet) {
            // 3.通過鍵獲取值
            String value = map.get(key);
            System.out.println(key + " === " + value);
        }

        // Entry方式
        // 1.獲取所有的Entry
        Set<Map.Entry<Student, String>> entrySet = map.entrySet();
        // 2.遍歷獲取每個Entry
        for (Map.Entry<Student, String> entry : entrySet) {
            // 3.通過Entry獲取鍵和值
            Student key = entry.getKey();
            String value = entry.getValue();
            System.out.println(key + " ::: " + value);
        }
    }

總結:
HashMap存儲自定義類型鍵需要怎麼做?

重寫hashCode和equals方法
只有對象放到哈希表結構(HashSet/HashMap)中才需要重寫hashCode和equals

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