2011-11-18 80 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