Update HashMap value
in
Programming Questions
•
1 year ago
Hi.
I'm trying to update the value of a hashmap key dynamically, in order to get the minimum and maximum values within a JSON object containing timbre information for an audio file.
There are 2 maps, to contain maximum and minimum values for each of the 12 timbres in the JSON file.
The map keys (t1, t2, tn) are initially given a value of 0. I want to update these values in both the maximum and minimum maps each time they exceed or go under whatever is their current value.
In the example below,
println("a: "+maxTimbres.get("t"+j)); returns (0), whereas
println("b: "+maxTimbres.get("t"+j)); returns the value that should have been updated in the hashmap.
How do I need to structure my code in order to achieve this? Is there a better way to approach it? I looked into dynamically creating variables but this seemed very complex in java.
thanks!
- // create HashMap to store max and min values for each of the 12 timbres
- HashMap<String, Float> maxTimbres=new HashMap<String,Float>();
- HashMap<String, Float> minTimbres=new HashMap<String,Float>();
- for (int i = 0; i < timbreSegments.get(0).size(); i++) {
- maxTimbres.put("t"+i, float(0));
- minTimbres.put("t"+i, float(0));
- }
- // Loop through JSON to get highest and lowest values to use as range
- for (int i = 0; i < timbreSegments.size(); i++) {
- for (int j = 0; j < timbreSegments.get(i).size(); j++) {
- println("a: "+maxTimbres.get("t"+j));
- if(timbreSegments.get(i).get(j) > maxTimbres.get("t"+j )) {
- maxTimbres.put("t"+j, timbreSegments.get(i).get(j));
- println("b "+maxTimbres.get("t"+j));
- } else if(timbreSegments.get(i).get(j) < minTimbres.get("t"+j)) {
- minTimbres.put("t"+j, timbreSegments.get(i).get(j));
- }
- }
- }
1