在下面的代码片段中,检查dic.isEmpty()是否会导致任何的性能改进?在输入while(Iterator.hasNext())循环之前Map.isEmpty()是否有意义?
for (Map<String, String> dic : dics) {
if (!dic.isEmpty()) {
Iterator<Map.Entry<String, String>> it = dic.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, String> pair = it.next();
Log.d("Substitute", pair.getKey() + " => " + pair.getValue());
}
}
}
毕竟,如果地图/ DIC是空的,而()循环将不进入,所以它看起来像支票dic.isEmpty()是多余的 - 除非有一些其他的理由它?
您的回复没有问题,但您的意思可能是“如果另一个线程删除了项目并在空白检查和迭代器抓取之间为空” – sactiw 2013-04-09 16:24:42
@sactiw是的,你说得很对。我的措辞需要在两行之间进行阅读,并感谢您填写“行间”。 – 2013-04-10 13:58:38