【发布时间】:2022-08-17 00:27:24
【问题描述】:
public HashMap<String, Integer> getAllCountryPopulations(){
List<CountryInfo> countries = countrySqlRepository.findAll();
HashMap<String, Integer> populations = new HashMap<>();
Integer sumOfPopulation = 0;
HashSet<String> set = new HashSet<String>();
for(int i=0; i<countries.size(); i++){
CountryInfo countryInfo = countries.get(i);
set.add(countryInfo.getCountryCode());
if(set.contains(countryInfo.getCountryCode())){
sumOfPopulation += Integer.parseInt(countryInfo.getStatePopulation().replaceAll(\",\", \"\"));
}
populations.put(countryInfo.getCountryCode(), sumOfPopulation);
}
return populations;
}
我正在尝试返回给定地图上唯一国家代码的值的总和。我没有返回集合中每个键的相应总和,而是得到集合中所有值的总和。
我如何在这里修复我的逻辑?
提前致谢。
-
你能澄清一下这个说法吗:
Instead of returning the corresponding sum for each key in the set I am getting the sum of all values within the set? -
最好提供
CountryInfo类的代码和样本数据代表输入,电流输出和期望的输出.