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小菜鸟的我还在研究中...