如何迭代嵌套的 TreeMap?

发布于 2024-10-27 01:39:22 字数 1410 浏览 0 评论 0原文

我有一组 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 技术交流群。

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

发布评论

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

评论(1

心如狂蝶 2024-11-03 01:39:22

不是很清楚(什么是 listOfDays、listOfCourts 和 listOfTimes?),但我猜你需要这样的东西:

for (Map.Entry<DayOfWeek, TreeMap<Court, TreeMap<Time, String>>> dayEntry : keyDay.entrySet()) {
    DayOfWeek currentDay = dayEntry.getKey();
    output += currentDay.toString() + "\n----------\n";
    for (Map.Entry<Court, TreeMap<Time, String>> courtEntry : dayEntry.getValue().entrySet()) {
        Court currentCourt = courtEntry.getKey();
        output += currentCourt.toString() + ":\n";
        for (Map.Entry<Time, String> timeEntry : currentCourt.getValue().entrySet()) {
            Time currentTime = timeEntry.getKey();
            String currentName = timeEntry.getValue();
            output += currentTime.toString() + " - " + currentName + "\n";
        }
    }
}

简而言之,地图可以被视为一组地图条目,并且你可以迭代这个条目集。每个条目都有一个键和一个值。由于在您的情况下,该值是另一个映射,因此您可以对该值重复此操作。

It's not very clear (what are listOfDays, listOfCourts and listOfTimes?), but I guess you need something like this:

for (Map.Entry<DayOfWeek, TreeMap<Court, TreeMap<Time, String>>> dayEntry : keyDay.entrySet()) {
    DayOfWeek currentDay = dayEntry.getKey();
    output += currentDay.toString() + "\n----------\n";
    for (Map.Entry<Court, TreeMap<Time, String>> courtEntry : dayEntry.getValue().entrySet()) {
        Court currentCourt = courtEntry.getKey();
        output += currentCourt.toString() + ":\n";
        for (Map.Entry<Time, String> timeEntry : currentCourt.getValue().entrySet()) {
            Time currentTime = timeEntry.getKey();
            String currentName = timeEntry.getValue();
            output += currentTime.toString() + " - " + currentName + "\n";
        }
    }
}

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.

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