Java 如何通过几个哈希映射执行算术运算?
目前我有6个哈希图,其中包含城市名称和不同类别的值,但我需要汇总每个类别的每个城市的值,即:Java 如何通过几个哈希映射执行算术运算?,java,android,collections,Java,Android,Collections,目前我有6个哈希图,其中包含城市名称和不同类别的值,但我需要汇总每个类别的每个城市的值,即: HashMap<String, Integer> HeatHMap = new HashMap<>(); HashMap<String, Integer> DaylightHMap = new HashMap<>(); HashMap<String, Integer> PrecitipationHMap = new HashMap<>
HashMap<String, Integer> HeatHMap = new HashMap<>();
HashMap<String, Integer> DaylightHMap = new HashMap<>();
HashMap<String, Integer> PrecitipationHMap = new HashMap<>();
HashMap<String, Integer> DaylightHMap = new HashMap<>();
HashMap<String, Integer> WindHMap = new HashMap<>();
HashMap<String, Integer> MoistureHMap = new HashMap<>();
DaylightHMap包含:
Cities Values
Tucson 23
Hermosillo 47
Boulder 25
Cities Values
Tucson 43
Hermosillo 37
Boulder 75
现在,我需要为每个类别将每个城市的值(即Hermosillo)相加,并将值保存到另一个HashMap中,因此结果如下:
Cities Values
Tucson 78 = (23+43+...+n)
Hermosillo 160 = (47+37+...+n)
....
我在考虑将每个HashMap添加到ArrayList中,然后访问每个城市,但我意识到将HashMap添加到列表中并不是解决此问题的好方法。到目前为止,我已经:
public void verifyTheWinner(
HashMap <String, Integer> Table1, HashMap <String, Integer> Table2,
HashMap <String, Integer> Table3, HashMap <String, Integer> Table4,
HashMap <String, Integer> Table5, HashMap <String, Integer> Table6)
{
List<HashMap> categories = new ArrayList<HashMap>();
categories.add(Weather);
categories.add(SeaWeather);
categories.add(Rainfall);
categories.add(Sunshine);
categories.add(Prices);
categories.add(AvgStd);
HashMap<String, Integer> citiesAndValuesTotal= new HashMap<>();
for (int i=0; i<categories.size(); i++){
......
}}
public void验证获胜者(
HashMap表1、HashMap表2、,
HashMap表3、HashMap表4、,
HashMap表5、HashMap表6)
{
列表类别=新建ArrayList();
类别。添加(天气);
类别。添加(海带);
增加(降雨量);
类别。添加(阳光);
类别。添加(价格);
类别。添加(AvgStd);
HashMap citiesAndValuesTotal=新HashMap();
对于(inti=0;i我将创建一个自定义对象(可能称为City
)来处理类似的事情
public class City {
public String name;
public Integer heat;
public Integer dayLight;
public Integer precipitation;
public Integer wind;
public Integer moisture;
public Integer getTotal() {
return heat + dayLight + precipitation + wind + moisture;
}
}
您可以有一张地图,从城市名称到城市
对象
另外,我认为你错贴了“沉淀”,有两个“白昼”贴图,你的参数名称与你在验证Winner
方法中使用的不匹配。我会制作一个自定义对象(可能称为City
)来处理类似的事情
public class City {
public String name;
public Integer heat;
public Integer dayLight;
public Integer precipitation;
public Integer wind;
public Integer moisture;
public Integer getTotal() {
return heat + dayLight + precipitation + wind + moisture;
}
}
您可以有一张地图,从城市名称到城市
对象
此外,我认为你错贴了“降水量”,有两张“白昼”地图,你的参数名称与你在验证Winner
方法中使用的不匹配。你最好使用更好的数据结构,例如一个包含任何给定城市所有需要的详细信息的类:
public class CityWeather {
private String name;
private int heat;
private int daylight;
// ...
private int moisture;
// ...
}
那么你只需要一张地图,比如说
然后,您不需要做任何特殊的操作来执行计算——只要您需要,它就在那里。您最好使用更好的数据结构,例如一个包含任何给定城市所有所需详细信息的类:
public class CityWeather {
private String name;
private int heat;
private int daylight;
// ...
private int moisture;
// ...
}
那么你只需要一张地图,比如说
然后,你不需要做任何特殊的事情来执行计算——只要你想,它就在那里。我非常感谢你的解决方案约翰·布林格和亚当·罗斯尼,但我能够使用这篇文章中的解决方案来解决这个问题
那更适合我的需要
我的解决办法如下:
//Solution from Melike Ttkn from the mentioned link
public HashMap<String, Integer> mergeAndAdd(ArrayList<HashMap<String, Integer>> maplist) {
HashMap<String, Integer> result = new HashMap<>();
for (HashMap<String, Integer> map : maplist) {
for (Map.Entry<String, Integer> entry : map.entrySet()) {
String key = entry.getKey();
Integer current = result.get(key);
result.put(key, current == null ? entry.getValue() : entry.getValue() + current);
}
}
return result;
}
我非常感谢你的解决方案约翰·博林格和亚当·罗斯尼,但我通过使用这篇文章中的解决方案解决了这个问题
那更适合我的需要
我的解决办法如下:
//Solution from Melike Ttkn from the mentioned link
public HashMap<String, Integer> mergeAndAdd(ArrayList<HashMap<String, Integer>> maplist) {
HashMap<String, Integer> result = new HashMap<>();
for (HashMap<String, Integer> map : maplist) {
for (Map.Entry<String, Integer> entry : map.entrySet()) {
String key = entry.getKey();
Integer current = result.get(key);
result.put(key, current == null ? entry.getValue() : entry.getValue() + current);
}
}
return result;
}
那么你打算为每个城市的日照时数、风速等增加温度?这有什么意义?嘿@JohnBollinger!我想添加的值只是为了测试目的,因为需要添加的表越多,包含的数据越有意义。这些只是为了测试目的。所以你打算添加每个城市的温度、日照时数、风速等?这有什么意义?嘿@JohnBollinger!我想添加的值只是为了测试目的,因为需要添加的表越多,包含的数据越有意义。这些只是为了测试目的。比我快一分钟!+1比我快一分钟! +1
System.out.println(mergeAndAdd(makeAlist(Values1, Values2, Values3, Values4, Values5, Values6)));