java.util.HashMap.remove()方法实例

java.util.HashMap.remove()方法实例

评论  编辑
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&ltu=http%3A%2F%2Fwww.yiibai.com%2Fjava%2Futil%2Fashmap_remove.html&ltr=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}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值