简体   繁体   English

如何通过几个HashMap执行算术运算?

[英]How to perform arithmetic operations through several HashMaps?

Currently I have 6 HashMaps that contain the name of cities and values of different categories but I need to sum up the values of each city for every category, that is: 目前,我有6个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<>();

Where HeatHMap contains: HeatHMap包含以下内容:

Cities    Values
Tucson     23
Hermosillo 47
Boulder    25

and DaylightHMap contains: 并且DaylightHMap包含:

Cities    Values
Tucson     43
Hermosillo 37
Boulder    75

Right now, I need to add up the values of each city, ie, Hermosillo, for each category and save the values into another HashMap, so the result would be something like: 现在,我需要为每个类别添加每个城市(即Hermosillo)的值,并将这些值保存到另一个HashMap中,因此结果将类似于:

 Cities    Values
 Tucson      78 =  (23+43+...+n)
 Hermosillo  160 = (47+37+...+n)
 ....

I was thinking in adding every HashMap into an ArrayList and then get access to each city but I realized having a HashMap into a list would not be a good approach to tackle this problem. 我当时在考虑将每个HashMap添加到ArrayList中,然后访问每个城市,但是我意识到将HashMap包含在列表中并不是解决此问题的好方法。 So far, I have: 到目前为止,我有:

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++){
       ......
    }}

My questions are: 我的问题是:

  1. How can I perform arithmetic operations such as addition of values for each city and then saving them into a new HashMap? 如何执行算术运算,例如为每个城市添加值,然后将其保存到新的HashMap中?

  2. Is there another Collection that I can use to accomplish this goal or is HashMap the best approach to solve this problem? 是否可以使用另一个Collection来实现此目标,还是HashMap是解决此问题的最佳方法?

Thanks for your support, please ask me for more details if you need them. 感谢您的支持,如果需要,请询问我更多详细信息。 Every answer is welcome. 欢迎每个答案。

You would be better off with a better data structure, such as a class that incorporated all the wanted details for any given city: 使用更好的数据结构会更好,例如,一个类合并了任何给定城市的所有所需详细信息:

public class CityWeather {
    private String name;
    private int heat;
    private int daylight;
    // ...
    private int moisture;
    // ...
}

Then you only need one map, say 然后,您只需要一张地图,说

HashMap<String, CityWeather> weatherMap = new HashMap<>();

That one map can meet your needs currently served by all the other ones, including the citiesAndValuesTotal map you want to create. 该地图可以满足您当前由其他所有地图提供的需求,包括您要创建的citiesAndValuesTotal地图。 For the last, all you need to do is add a method to CityWeather , something like this: 最后,您需要做的就是向CityWeather添加方法,如下所示:

int meaninglessWeatherSum() {
    return heat + daylight + ... + moisture;
}

Then you don't need to do anything special to perform the computation -- it's right there whenever you want it. 然后,您无需执行任何特殊操作即可执行计算-随时随地都可以在其中进行。

I would make a custom object (maybe called City ) to handle something like this. 我将创建一个自定义对象(可能称为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;
  }

}

You could have a single map, from the city name to the City object. 您可能只有一张地图,从城市名称到City对象。

Also I think you mispelled "precipitation", have two "day light" maps and your parameter names don't match what you use in your verifyTheWinner method. 另外,我认为您拼写了“降水”,有两个“日光”图,并且参数名称与您在verifyTheWinner方法中使用的名称不匹配。

I appreciate very much your solutions John Bollinger and Adam Rosni, but I was able to solve this problem by using the solution from this post how to merge more than one hashmaps also sum the values of same key in java that suited better my needs. 我非常感谢您的解决方案John Bollinger和Adam Rosni,但我能够通过使用本文中的解决方案来解决此问题,该方法如何合并多个hashmaps并汇总java中相同键的值,这些值更适合我的需求。

My solution was the following: 我的解决方案如下:

//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;
}

Then I added my method to make a listArray of HashMaps like this: 然后,我添加了我的方法,以使HashMaps的listArray像这样:

public ArrayList<HashMap<String,Integer>> makeAlist(HashMap<String,Integer> Values1, HashMap<String, Integer> Values2, HashMap<String, Integer> Values3,
HashMap<String, Integer> Values4, HashMap <String, Integer> Values5, HashMap<String, Integer> Values6){

ArrayList<HashMap<String,Integer>> mergedCategories = new ArrayList<>();

    mergedCategories.add(Values1);
    mergedCategories.add(Values2);
    mergedCategories.add(Values3);
    mergedCategories.add(Values4);
    mergedCategories.add(Values5);
    mergedCategories.add(Values6);

    return mergedCategories;
}

And lastly, I just called this method: 最后,我只是调用了此方法:

System.out.println(mergeAndAdd(makeAlist(Values1, Values2, Values3, Values4, Values5, Values6)));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM