简体   繁体   English

如何遍历嵌套的TreeMap?

[英]How do I iterate through nested TreeMaps?

I have a set of 3 nested TreeMaps: 我有一组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>();

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. 我试图使用嵌套的while循环来遍历它们,以显示所有已创建的预订,但是我需要一种在嵌套的whiles中显示仅与相关父代相关的值的方法。

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. ...但是按预期(但不希望),它只是遍历每个TreeMap中的所有条目,直到最后。

Can anyone help? 有人可以帮忙吗?

It's not very clear (what are listOfDays, listOfCourts and listOfTimes?), but I guess you need something like this: 还不是很清楚(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";
        }
    }
}

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. 由于该值(在您的情况下)是另一个映射,因此您可以对该值重复此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM