Is there any way to set the accumulator to 0 after every time we use it and after that it still performs the same function? or How to calculate the overall value for each key in a Map like this : String, Hashmap String, Integer ? I am trying to be as clarifying as I can but I don't have a clue about that.
/*EXAMPLE OF HASHMAP: String e.g - UK ; America ; Africa , etc. String e.g - black , white, asian Integer e.g- 2008 , 103432 , 2391 // for every country the values are diff
*/
//one way to think of this is maybe with accumulator
// THIS IS AN EXAMPLE CODE WHICH DOES NOTHING.
int acc = 0 ;
for ( int i = 0 ; i < 20 ; i++ ) {
if ( i == 1 ) {
acc++; } // after acc ++ and it is 1, I want it to be again 0;
if ( i == 3 ) {
acc++; } // so that on this stage it will be 1
}
//Another alternative way may be:
Map<String,HashMap<String,Integer>> question = new HashMap<>();
int count = 0 ;
for(String regions : question.keySet())
{
for (String people : question.get(regions).keySet())
{
count = count + question.get(regions).get(people); // and here it will count all the value but I want to count it for each region and then become to 0 again, so that I can have the overall value for each of the people(keys)
}
}
Your question is a bit unclear but, here is an example that calculates values for a key in hashmap. Let's assume we have
Where X, Y and Z are of type String and their values are of type Integer. The following code, stores the above structure and then calculate X,Y,Z for US and UK.
the output: