扫描二维码下载沐宇APP

沐宇

微信扫码使用沐宇小程序

沐宇

hashmap鎬庝箞鍘婚櫎鎸囧畾鍏冪礌

扬州沐宇科技
2024-06-03 17:36:09
hashmap

瑕佷粠HashMap涓垹闄ゆ寚瀹氱殑鍏冪礌锛屽彲浠ヤ娇鐢ㄤ互涓嬫柟娉曪細

  1. 浣跨敤remove(key)鏂规硶锛氫娇鐢℉ashMap鐨剅emove(key)鏂规硶鍙互鍒犻櫎鎸囧畾閿墍瀵瑰簲鐨勯敭鍊煎銆傜ず渚嬩唬鐮佸涓嬶細
HashMap<String, Integer> hashMap = new HashMap<>();
hashMap.put("A", 1);
hashMap.put("B", 2);

hashMap.remove("A");

System.out.println(hashMap); // 杈撳嚭锛歿B=2}
  1. 浣跨敤remove(key, value)鏂规硶锛氬鏋滈渶瑕佸悓鏃舵寚瀹氶敭鍜屽€兼潵鍒犻櫎鍏冪礌锛屽彲浠ヤ娇鐢╮emove(key, value)鏂规硶銆傜ず渚嬩唬鐮佸涓嬶細
HashMap<String, Integer> hashMap = new HashMap<>();
hashMap.put("A", 1);
hashMap.put("B", 2);

hashMap.remove("A", 1);

System.out.println(hashMap); // 杈撳嚭锛歿B=2}
  1. 浣跨敤Iterator閬嶅巻骞跺垹闄わ細鍙互浣跨敤Iterator閬嶅巻HashMap骞跺垹闄ゆ寚瀹氬厓绱犮€傜ず渚嬩唬鐮佸涓嬶細
HashMap<String, Integer> hashMap = new HashMap<>();
hashMap.put("A", 1);
hashMap.put("B", 2);

Iterator<Map.Entry<String, Integer>> iterator = hashMap.entrySet().iterator();
while (iterator.hasNext()) {
    Map.Entry<String, Integer> entry = iterator.next();
    if (entry.getKey().equals("A")) {
        iterator.remove();
    }
}

System.out.println(hashMap); // 杈撳嚭锛歿B=2}

浠ヤ笂鏄嚑绉嶅父瑙佺殑鏂规硶鏉ュ垹闄ashMap涓殑鎸囧畾鍏冪礌锛岄€夋嫨閫傚悎鑷繁闇€姹傜殑鏂规硶鏉ュ疄鐜板厓绱犵殑鍒犻櫎銆?/p>

扫码添加客服微信