使用该remove()
方法从HashMap中删除值。
首先,创建一个HashMap并添加元素-
HashMap hm = new HashMap(); hm.put("Wallet", new Integer(700)); hm.put("Belt", new Integer(600)); hm.put("Backpack", new Integer(1200));
现在,删除一个值,比如用键“ Wallet”-
Object ob = hm.remove("Wallet");
以下是从HashMap中删除值的示例-
import java.util.*; public class Demo { public static void main(String args[]) { //创建哈希映射 HashMap hm = new HashMap(); hm.put("Wallet", new Integer(700)); hm.put("Belt", new Integer(600)); hm.put("Backpack", new Integer(1200)); System.out.println("Map = "+hm); Object ob = hm.remove("Wallet"); System.out.println("Map after removing an element= "+hm); } }
输出结果
Map = {Backpack=1200, Belt=600, Wallet=700} Map after removing an element= {Backpack=1200, Belt=600}