Bug:Performance - Inefficient use of keySet iterator instead of entrySet iterator
描述:This method accesses the value of a Map entry, using a key that was retrieved from a keySet iterator. It is more efficient to use an iterator on the entrySet of the map, to avoid the Map.get(key) lookup.
根据上面的英文描述,相比于entryset来说,keyset是低效的,虽然keyset写起来更方便,但考虑到实际性能,还是用entryset对其进行替换更好。
keyset迭代器:
Map<String, String> param = new HashMap<>();
//开始循环
for (String key : param.keySet()) {
list.add(new BasicNameValuePair(key, param.get(key)));
}
entryset迭代器:
Map<String, String> param = new HashMap<>()
//请求参数转换
for (Map.Entry<String, String> entry : param.entrySet()) {
list.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}