简体   繁体   中英

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:

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:

Cities    Values
Tucson     23
Hermosillo 47
Boulder    25

and DaylightHMap contains:

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:

 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. 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?

  2. Is there another Collection that I can use to accomplish this goal or is HashMap the best approach to solve this problem?

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. For the last, all you need to do is add a method to CityWeather , something like this:

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.

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.

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.

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.

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:

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)));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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