I have a problem with changing values in an HashMap>. First I do this:
HashMap<String, ArrayList<Integer>> countries = new HashMap<>();
ArrayList<Integer> medals = new ArrayList<>();
medals.add(0); medals.add(0); medals.add(0);
for(int i = 0; i < COUNTRY.length; i++){
countries.put(COUNTRY[i], medals);
}
I fill the HashMap with keys from a static array and add a default value of an ArrayList that is filled with 0, 0, 0. Then I do this:
Integer plus = new Integer(1);
for(int j = 0; j < athletes.size(); j++){
if(j == 3)
break;
Athlete a = athletes.get(j);
ArrayList<Integer> medals = countries.get(a.getCountry());
Integer medal = medals.get(j) + plus;
medals.set(j, medal);
countries.put(a.getCountry(), medals);
}
I have an ArrayList filled with athletes that is already sorted by the athlete's results. What I'm trying to do is take the top 3 placed athletes, and update my HashMap value so the ArrayList will have 3 numbers that represent how many gold, silver and bronze medals every country has won. The problem is that when I try to replace the old value with a new ArrayList it replaces all the values in the HashMap with new ArrayList's instead of just the one with the matching key.
I have no idea what the problem is, any suggestions??