更新Map键值java

时间:2011-11-18 10:34:01

标签: java treemap

好的我有这段代码:

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;

它检查子图中的日期范围。然后检查这些日期的值(键是btw)是否可以容纳终端(即预订号),如果是,它将从当前地图中的容量中减去该值。我不确定如何使用值

更新startx和endx之间所有日期的地图容量
capacity -= terminals;

谢谢, :)

1 个答案:

答案 0 :(得分:9)

您必须使用更新后的值将键/值重新插入地图。

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