这段代码抛出错误 此方法使用从keySet迭代器检索的键访问Map条目的值。在map的entrySet上使用迭代器更有效,以避免Map.get(key)查找。 请指导我如何改写它
if (docPropertiesMap != null) {
Iterator<String> properties = docPropertiesMap.keySet().iterator();
IDocProperty[] docProperties = new IDocProperty[docPropertiesMap
.size()];
int iArrIndex = 0;
while (properties.hasNext()) {
String strPropName = properties.next();
String[] propValue = docPropertiesMap.get(strPropName);
IDocProperty docProperty = (IDocProperty) FDMAFactory
.getDataObject("DocProperty");
docProperty.setPropertyName(strPropName);
docProperty.setArrPropertyValues(propValue);
docProperties[iArrIndex++] = docProperty;
}
metadata.setArrDocProperties(docProperties);
return metadata;
}
答案 0 :(得分:3)
这是来自FindBugs的SonarQube警告。
您可以像这样重写代码:
if (docPropertiesMap != null) {
IDocProperty[] docProperties = new IDocProperty[docPropertiesMap.size()];
int iArrIndex = 0;
for (Map.Entry<String, String[]> entry : docPropertiesMap.entrySet()) {
String strPropName = entry.getKey();
String[] propValue = entry.getValue();
IDocProperty docProperty = (IDocProperty) FDMAFactory.getDataObject("DocProperty");
docProperty.setPropertyName(strPropName);
docProperty.setArrPropertyValues(propValue);
docProperties[iArrIndex++] = docProperty;
}
metadata.setArrDocProperties(docProperties);
return metadata;
}