2011-11-18 71 views
3

好吧,我有这样的代码:更新地图键的值的java

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; 

谢谢, :)

回答

9

你必须键/值重新插入与更新地图值。

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

如何在这种情况下访问密钥?因为我的所有密钥都是DateTime类型...任何提示? –

+0

不要遍历这些值,而是循环遍历子映射的键。 (从关键到价值很容易。) – aioobe

+0

@sys_debug'tree2.subMap(startx,endx).keySet()'? – Thomas