java中map的逆序遍历_java中map的遍历

java中map的遍历

2012/2/28 18:01:30  vanceinfo_xuefei  程序员俱乐部  我要评论(0)

摘要:今天写完代码做findbugs时在map的遍历这方面出现了一下的一个提示:”inefficientuseofkeySetiteratorinsteadofentrySetiterator“大概意思就是效率不高。经过研究比较,发现以下两种方式遍历map都可以,只是效率不同而已MapcatalogIds=newHashMap();方式一、Set

今天写完代码做find bugs时在map的遍历这方面出现了一下的一个提示:

”inefficient use of keySet iterator instead of entrySet iterator“

大概意思就是效率不高。

经过研究比较,发现以下两种方式遍历map都可以,只是效率不同而已

Map catalogIds = new HashMap();

方式一、

Set> set = catalogIds.entrySet();

Iterator> it = set.iterator();

while (it.hasNext())

{

String catalogId = it.next().getKey();

if (catalogIds.get(catalogId) < 2)

{

it.remove();

catalogIds.remove(catalogId);

}

}

方式二、

Iterator it = catalogIds.keySet().iterator();

while (it.hasNext())

{

String catalogId = it.next();

if (catalogIds.get(catalogId) < 2)

{

it.remove();

catalogIds.remove(catalogId);

}

}

根据find bugs提示来看,方式一比方式二的效率更高...至于为什么,作为java小菜鸟的我还在研究中...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值