java更新Map键值

发布于 2024-12-16 21:19:57 字数 996 浏览 2 评论 0原文

好的,我有这段代码:

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;

它检查子图中的日期范围。然后检查这些日期的值(顺便说一句,它们是键)是否可以容纳终端(即预订号),如果是,则将从地图中当前的容量中减去该值。我不确定如何更新地图中 startx 和 endx 之间所有日期的容量值

capacity -= terminals;

谢谢, :)

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,
:)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

世俗缘 2024-12-23 21:19:57

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

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

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

tree2.put(key, tree2.get(key) - terminals);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文