Java HashMap 集合

Java HashMap 集合


HashMap

  • 繼承自 AbstractMap

  • 以鍵值對形式存儲元素

  • 鍵不可重複,值可以重複

  • 根據哈希算法和equals方法的比較,保證鍵不重複

  • HashMap 是線程不安全的

  • 從JDK 1.2 開始

使用方法

  • 添加元素

    • 示例

      HashMap<String, Integer> hm = new HashMap<>();
      
      // 添加指定的鍵值對元素到集合中
      hm.put("電冰箱", 1000);
      hm.put("洗衣機", 900);
      hm.put("電視機", 1000);
      hm.put("空調", 1100);
      hm.put("熱水器", 1400);
      
      for(Entry<String, Integer> kv : hm.entrySet()) {
          System.out.println(kv.getKey() + ":" + kv.getValue());
      }
    • 運行結果
      這裏寫圖片描述

  • 修改元素

    • 示例

      HashMap<String, Integer> hm = new HashMap<>();
      
      hm.put("電冰箱", 1000);
      hm.put("洗衣機", 900);
      hm.put("電視機", 1000);
      hm.put("空調", 1100);
      hm.put("熱水器", 1400);
      
      // 把電冰箱的價格修改成1500元
      hm.put("電冰箱", 1500);
      
      for(Entry<String, Integer> kv : hm.entrySet()) {
          System.out.println(kv.getKey() + ":" + kv.getValue());
      }
    • 運行結果
      這裏寫圖片描述

  • 獲取元素

    • 示例

      HashMap<String, Integer> hm = new HashMap<>();
      
      hm.put("電冰箱", 1000);
      hm.put("洗衣機", 900);
      hm.put("電視機", 1000);
      hm.put("空調", 1100);
      hm.put("熱水器", 1400);
      
      // 獲得洗衣機的價格
      int price = hm.get("洗衣機");
      
      System.out.println(price);
    • 運行結果
      這裏寫圖片描述

  • 移除元素

    • 示例

      HashMap<String, Integer> hm = new HashMap<>();
      
      hm.put("電冰箱", 1000);
      hm.put("洗衣機", 900);
      hm.put("電視機", 1000);
      hm.put("空調", 1100);
      hm.put("熱水器", 1400);
      
      // 移除 電冰箱和空調
      hm.remove("電冰箱");
      hm.remove("空調");
      
      for(Entry<String, Integer> kv : hm.entrySet()) {
          System.out.println(kv.getKey() + ":" + kv.getValue());
      }
      
      System.out.println("---------------");
      
      // 移除所有元素
      hm.clear();
      
      System.out.println(hm.size());
    • 運行結果
      這裏寫圖片描述

  • 判斷相關

    • 示例

      HashMap<String, Integer> hm = new HashMap<>();
      
      hm.put("電冰箱", 1000);
      hm.put("洗衣機", 900);
      hm.put("電視機", 1000);
      hm.put("空調", 1100);
      hm.put("熱水器", 1400);
      
      // 判斷是否包含 “空調” 的鍵
      boolean result = hm.containsKey("空調");
      System.out.println("containsKey(Object key) :" + result);
      
      // 判斷是否包含 “1000” 的值
      result = hm.containsValue(1000);
      System.out.println("containsValue(Object value) :" + result);
      
      // 判斷集合是否爲空
      result = hm.isEmpty();
      System.out.println("isEmpty() :" + result);
    • 運行結果
      這裏寫圖片描述

遍歷集合元素

  • keySet 方式遍歷

    • 調用keySet() 方法,返回一個Set集合,這個集合中存儲了所有的鍵,然後遍歷Set集合,通過鍵獲得值。
    • 示例

      HashMap<String, Integer> hm = new HashMap<>();
      
      hm.put("電冰箱", 1000);
      hm.put("洗衣機", 900);
      hm.put("電視機", 1000);
      hm.put("空調", 1100);
      hm.put("熱水器", 1400);
      
      Set<String> keySet = hm.keySet();
      for(String key : keySet) {
          System.out.println(key + " : " + hm.get(key));
      }
    • 運行結果
      這裏寫圖片描述

  • entrySet 遍歷

    • 調用 entrySet() 方法,獲得所有的鍵值對
    • 示例

      HashMap<String, Integer> hm = new HashMap<>();
      
      hm.put("電冰箱", 1000);
      hm.put("洗衣機", 900);
      hm.put("電視機", 1000);
      hm.put("空調", 1100);
      hm.put("熱水器", 1400);
      
      for(Entry<String, Integer> kv : hm.entrySet()) {
          System.out.println(kv.getKey() + " : " + kv.getValue());
      }
    • 運行結果
      這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章