如何创建嵌套 TreeMap?
这与我之前的问题有些相关,但我意识到我需要在问题的早期处理嵌套问题,所以这就是我所在的位置。 (我距离 Java 初学者也已经不远了,所以请耐心等待)。
我正在为 5 个房间创建一个简单的预订系统,其中包含姓名、时间、房间号和天数。这必须使用嵌套的 TreeMap 来完成。这是我看到的数据布局,其中圆括号代表 TreeMap 的边界:
(Day, (Room #, (Time, Name)))
据我所知,我需要一个 TreeMap 来存储时间和名称,每个房间一个,然后每天一个。这意味着每天每个房间有一个时间/名称树状图,即 1 x 5 x 7 = 35 个树状图。像这样:(
{Mon, [Room 1, (0600, NameA
0630, NameB
0700, NameC)
Room 2, (0600, NameD
0630, NameE)
Room 3, (0600, NameF
0630, NameG)]
Tues, [Room 1, (0600, Name1
0630, Name2)
Room 2, (0600, Name3
0630, Name4
0700, Name5)]}
不同的括号类型代表嵌套 TreeMap 的边界)
得出这个结论后,我的下一个问题是迭代循环以创建所有这些 TreeMap。我似乎无法使用 for 循环动态生成 TreeMap,因为我无法将计数器的变量号粘贴到新创建的 TreeMap 的名称上。
我确实有这个:
TreeMap keyDay = new TreeMap();
TreeMap keyRoom = new TreeMap();
TreeMap keyTime = new TreeMap();
但它只有三个,这显然不足以允许重复键 - 任何新条目,例如“0900”(时间键)或“房间 1”(房间键)将覆盖旧条目。
有人有什么建议吗?将不胜感激:)
This is somewhat related to my previous question but I've realised that I needed to deal with the issue of nesting earlier in the problem, so here's where I am. (I'm also not far off being a beginner in Java, so please bear with me).
I'm creating a simple booking system for 5 rooms which will take in names, times, room numbers and days. This has to be done using nested TreeMaps. Here's the layout of the data as I see it, where paretheses represent the boundaries of a TreeMap:
(Day, (Room #, (Time, Name)))
As far as I can see, I need one TreeMap for times and names, one for each room, then one for each day. That means one time/name treemap per room per day, which means 1 x 5 x 7 = 35 TreeMaps. Like this:
{Mon, [Room 1, (0600, NameA
0630, NameB
0700, NameC)
Room 2, (0600, NameD
0630, NameE)
Room 3, (0600, NameF
0630, NameG)]
Tues, [Room 1, (0600, Name1
0630, Name2)
Room 2, (0600, Name3
0630, Name4
0700, Name5)]}
(the different bracket types represent the boundaries of the nested TreeMaps)
Having come to that conclusion, my next problem is iterating through a loop to create all those TreeMaps. I can't seem to dynamically generate the TreeMaps using a for loop, because I can't stick a counter's variable number onto the newly-created TreeMap's name.
I did have this:
TreeMap keyDay = new TreeMap();
TreeMap keyRoom = new TreeMap();
TreeMap keyTime = new TreeMap();
but it is only three, which is clearly not enough to allow for duplication of keys - any new entries for e.g. '0900' (time key) or e.g. 'Room 1' (room key) will overwrite the old ones.
Does anyone have any suggestions? Would be much appreciated :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我同意这真的非常糟糕。但如果你想实现这个功能,我推荐使用HashMaps。这是一个实现您想要的功能的类:
}
这是一个运行 Reservations 类的 main:
最后,使用 SetupMap 和单个条目,它会生成:
该结果是在不到一秒的时间内生成的。我保证这比嵌套的 TreeMap 更高效。祝你好运!
I agree it is really super mega bad. But if you want to implement this functionality, I recommend HashMaps instead. Here is a class that implements the functionality you want:
}
Here is a main that runs the Reservations class:
And finally, using SetupMap and the single entry, it produces:
That result was generated in less than a single second. I promise that is infinitely more efficient than nested TreeMaps. Good Luck!
人们会假设这一天实际上是一个枚举,因此顶级映射应该是一个 EnumMap
One would assume that the day is actually an enum, so the top level map should be an EnumMap
并不是真正的答案,而是一种想法;)
确实,正如 xappymah 在评论中指出的那样,更自然的方法是为您的任务发明特定于域的类型。喜欢:
Not really an answer, but rather a thought ;)
Indeed, as xappymah pointed out in comments, more natural approach would be to invent domain-specific types for your task. Like: