如何迭代嵌套的 TreeMap?
我有一组 3 个嵌套的 TreeMap:
TreeMap<DayOfWeek, TreeMap<Court, TreeMap<Time, String>>> keyDay = new TreeMap<DayOfWeek, TreeMap<Court, TreeMap<Time, String>>>();
TreeMap<Court, TreeMap<Time, String>> keyCourt = new TreeMap<Court, TreeMap<Time, String>>();
TreeMap<Time, String> keyTime = new TreeMap<Time, String>();
用于存储预订信息。我正在尝试使用嵌套 while 循环来迭代它们,以显示已创建的所有预订,但我需要一种在嵌套 while 中仅显示与相关父级相关的值的方法。
这就是我所拥有的:
Iterator listOfDays = keyDay.keySet().iterator();
Iterator listOfCourts = keyCourt.keySet().iterator();
Iterator listOfTimes = keyTime.keySet().iterator();
String output;
while (listOfDays.hasNext()) {
DayOfWeek currentDay = (DayOfWeek) (listOfDays.next());
output += currentDay.toString() + "\n----------\n";
while (listOfCourts.hasNext()){
Court currentCourt = (Court) (listOfCourts.next());
output += currentCourt.toString() + ":\n";
while (listOfTimes.hasNext()){
Time currentTime = (Time) (listOfTimes.next());
String currentName = (String) keyTime.get(currentTime);
output += currentTime.toString() + " - " + currentName + "\n";
}
}
...但是正如预期的那样(但不是想要的),它只是迭代每个 TreeMap 中的所有条目直到最后。
有人可以帮忙吗?
I have a set of 3 nested TreeMaps:
TreeMap<DayOfWeek, TreeMap<Court, TreeMap<Time, String>>> keyDay = new TreeMap<DayOfWeek, TreeMap<Court, TreeMap<Time, String>>>();
TreeMap<Court, TreeMap<Time, String>> keyCourt = new TreeMap<Court, TreeMap<Time, String>>();
TreeMap<Time, String> keyTime = new TreeMap<Time, String>();
which store info for bookings. I'm trying to iterate through them using nested while loops to show all the bookings that have been created, but I need a way in the nested whiles to show only the values which relate to the relevant parent.
Here's what I have:
Iterator listOfDays = keyDay.keySet().iterator();
Iterator listOfCourts = keyCourt.keySet().iterator();
Iterator listOfTimes = keyTime.keySet().iterator();
String output;
while (listOfDays.hasNext()) {
DayOfWeek currentDay = (DayOfWeek) (listOfDays.next());
output += currentDay.toString() + "\n----------\n";
while (listOfCourts.hasNext()){
Court currentCourt = (Court) (listOfCourts.next());
output += currentCourt.toString() + ":\n";
while (listOfTimes.hasNext()){
Time currentTime = (Time) (listOfTimes.next());
String currentName = (String) keyTime.get(currentTime);
output += currentTime.toString() + " - " + currentName + "\n";
}
}
...but as expected (but not wanted), it just iterates through all the entries in each TreeMap til the end.
Can anyone help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不是很清楚(什么是 listOfDays、listOfCourts 和 listOfTimes?),但我猜你需要这样的东西:
简而言之,地图可以被视为一组地图条目,并且你可以迭代这个条目集。每个条目都有一个键和一个值。由于在您的情况下,该值是另一个映射,因此您可以对该值重复此操作。
It's not very clear (what are listOfDays, listOfCourts and listOfTimes?), but I guess you need something like this:
In short, a map can be viewed as a set of map entries, and you can iterate through this entry set. Each entry has a key and a value. Since the value, in your case, is another map, you can repeat this operation on the value.