hashmap鎬庝箞鍘婚櫎鎸囧畾鍏冪礌
瑕佷粠HashMap涓垹闄ゆ寚瀹氱殑鍏冪礌锛屽彲浠ヤ娇鐢ㄤ互涓嬫柟娉曪細
- 浣跨敤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}
- 浣跨敤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}
- 浣跨敤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>
相关问答