java hashtable 删除_Java hashtable.remove()

publicvoidbuy(Integerid,Stringtype){if("buy".equals(type)){if(hashtable.containsKey(id)){ShopItemitem=(ShopItem)hashtable.get(id);item.increment();}else{Foodfood=foodBiz....

public void buy(Integer id, String type) { if ("buy".equals(type)) { if (hashtable.containsKey(id)) { ShopItem item = (ShopItem) hashtable.get(id); item.increment(); } else { Food food = foodBiz.getFoodById(id); ShopItem item = new ShopItem(food); hashtable.put(id, item); } System.out.println("" + hashtable.size()); } else { System.out.println("buy" + hashtable.size()); hashtable.remove(id); System.out.println(hashtable.size()); } } 删除不了 hashtable里的对应id 的数据 由于我不知道你完整的程序是什么,所以没办法说你为什么删不掉 我学java的时候也遇到过这种情况 你可以这样删 先Set set=hashtable.keySet(); Iterator it= set.iterator (); while(it.hasNext()){ int a=next(); if(a==id){ it.remove(a); } } 就是先调用keySet方法返回一个hashmap的key的set视图, 然后调用set的迭代器,然后在迭代器中找到key,然后调用迭代器中的remove方法 这样绝对可以删掉了

展开

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值