remove() 方法用于从该映射中移除了映射指定键(如果存在)。
声明
以下是java.util.HashMap.remove()方法的声明。
public V remove(Object key)
参数 key–这是其映射关系要从映射中移除的键对应值。
返回值 该方法调用返回与key相关联的值。返回null,如果没有键的映射关系的前一个值。 异常 NA
例子 下面的例子显示java.util.HashMap.remove()方法的使用package com.yiibai;
import java.util.*;
public class HashMapDemo {
public static void main(String args[]) {
// create hash map
HashMap newmap = new HashMap();
// populate hash map
newmap.put(1, "tutorials");
newmap.put(2, "point");
newmap.put(3, "is best");
System.out.println("Values before remove: "+ newmap);
// remove value for key 2
newmap.remove(2);
System.out.println("Values after remove: "+ newmap);
}
} 现在编译和运行上面的代码示例,将产生以下结果。Values before remove: {1=tutorials, 2=point, 3=is best}
Values after remove: {1=tutorials, 3=is best}