id="iframeu1064372_0" src="http://pos.baidu.com/ncpm?rdid=1064372&dc=2&di=u1064372&dri=0&dis=0&dai=1&ps=323x335&dcb=BAIDU_SSP_define&dtm=BAIDU_DUP_SETJSONADSLOT&dvi=0.0&dci=-1&dpt=none&tsr=0&tpr=1459144932123&ti=java.util.HashMap.remove()%E6%96%B9%E6%B3%95%E5%AE%9E%E4%BE%8B&ari=1&dbv=2&drs=1&pcs=1156x572&pss=1156x1476&cfv=0&cpl=4&chi=1&cce=true&cec=UTF-8&tlm=1459144932<u=http%3A%2F%2Fwww.yiibai.com%2Fjava%2Futil%2Fashmap_remove.html<r=https%3A%2F%2Fwww.baidu.com%2Flink%3Furl%3DVkT4Wu3jerwwEb_aVTyNxDl-nqpX4imXbibIKyRBSVo1lVaC4ooKlwSDMtzqaESxMxKRWWdZ-TDR7Tva4Eb2B_%26wd%3D%26eqid%3Df3f6d89b0001bfd40000000556f8c8a5&ecd=1&psr=1366x768&par=1366x728&pis=-1x-1&ccd=24&cja=false&cmi=6&col=zh-CN&cdo=-1&tcn=1459144932&qn=9a4841f476805c55&tt=1459144932000.126.312.314" width="728" height="90" align="center,center" vspace="0" hspace="0" marginwidth="0" marginheight="0" scrolling="no" frameborder="0" allowtransparency="true" style="border-width: 0px; border-style: initial; vertical-align: bottom; margin: 0px;">
描述
The remove() method is used to remove the mapping for the specified key from this map if present.
声明
Following is the declaration for java.util.HashMap.remove() method.
public V remove(Object key)
参数
-
key--This is the key whose mapping is to be removed from the map.
返回值
The method call returns the previous value associated with key, or null if there was no mapping for key.
异常
-
NA
以下例子将告诉你如何使用 java.util.HashMap.remove()
package yiibai.com; 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); } }
Let us compile and run the above program, this will produce the following result.
Values before remove: {1=tutorials, 2=point, 3=is best} Values after remove: {1=tutorials, 3=is best}