update Map key's value java

sys_debug picture sys_debug · Nov 18, 2011 · Viewed 19.7k times · Source

Ok I have this code:

TreeMap<DateTime, Integer> tree2 = getDatesTreeMap();
DateTime startx = new DateTime(startDate.getTime());
DateTime endx = new DateTime(endDate.getTime());
boolean possible = false;
int testValue = 0;
//produces submap
Map<DateTime, Integer> nav = tree2.subMap(startx, endx);

for (Integer capacity : tree2.subMap(startx, endx).values()) {
    //Provides an insight into capacity accomodation possibility
    //testValue++;
    terminals = 20;
    if(capacity >= terminals)
        possible = true;
    else if(capacity < terminals)
        possible = false;

}

if(possible == true)
{
    for (Integer capacity : tree2.subMap(startx, endx).values()) {
    {
        capacity -= terminals;
        //not sure what to do
    }
}
}else{

}

return possible;

It checks for range of date in submap. then checks if values of those dates (which are keys btw) can accomodate terminals (that is reservation number), then if yes it would subtract that from capacity currently in map. I am unsure how to update the capacity in the map for all dates between startx and endx with value

capacity -= terminals;

Thanks, :)

Answer

aioobe picture aioobe · Nov 18, 2011

You have to reinsert the key / value into the map with the updated value.

tree2.put(key, tree2.get(key) - terminals);