gpt4 book ai didi

java - 循环3个map获取同一个key的值

转载 作者:行者123 更新时间:2023-11-30 08:08:06 28 4
gpt4 key购买 nike

我正在尝试将所有值放入 3 个 map directionMap 中。 JSONArray stop 中的 DirectionMap2、directionMap3。在这些映射中,所有键 name (stop_name) 都是相同的,但值 timeEntries、timeEntries2、timeEntries3 (mon-fri、sat、son) 不同,所以我想将所有这些值放在同一个 JSONObject arrivals 中,但我面临循环遍历所有这 3 个映射以获取同一键的所有值的问题?

我尝试将 3 个 for 循环放在一起,但这不是正确的解决方案。

感谢任何帮助。

结果应该看起来很简单:

[
{
"arrival_time": {"mon-fri": ["04:24","05:10","05:40"],
"sat": ["05:34","05:55","06:15"],
"son": ["07:00","08:00","05:40"]

}
"stop_name": "garden street"
},
{
"arrival_time": {"mon-fri": ["04:24","05:10","05:40"],
"sat": ["05:34","05:55","06:15"],
"son": ["07:00","08:00","05:40"]

}
"stop_name": "luise street"
}
]

代码:

  JSONArray stops = new JSONArray();

for (int index = 0; index < mainList.size(); index += 3) {

RouteCreator route_mon_fri = mainList.get(index);
Map<String, List<String>> directionMap = route_mon_fri
.getDirectionMap();
String direction = route_mon_fri.getDirection();
String route = route_mon_fri.getRoute();

RouteCreator route_sat = mainList.get(index + 1);
Map<String, List<String>> directionMap2 = route_sat
.getDirectionMap();

RouteCreator route_son = mainList.get(index + 2);
Map<String, List<String>> directionMap3 = route_son
.getDirectionMap();

List<String> timeEntries;
for (Entry<String, List<String>> entry : directionMap.entrySet()) {
String name = entry.getKey().trim();
timeEntries = entry.getValue();

try {
JSONObject stop = new JSONObject();

JSONObject arrivals = new JSONObject();
JSONArray timeArray = new JSONArray(timeEntries);
//JSONArray timeArray2 = new JSONArray(timeEntries2);
//JSONArray timeArray3 = new JSONArray(timeEntries3);
arrivals.put("mon-fri", timeArray);
//arrivals.put("sat", timeArray2);
//arrivals.put("son", timeArray3);

stop.put("arrival_time", arrivals);
stop.put("stop_name", name);
stops.put(stop);
System.out.println(stops.toString(3));

} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
List<String> timeEntries2;
for (Entry<String, List<String>> entry2 : directionMap2.entrySet()) {
timeEntries2 = entry2.getValue();
}
List<String> timeEntries3;
for (Entry<String, List<String>> entry3 : directionMap3.entrySet()) {
timeEntries3 = entry3.getValue();

}


}

最佳答案

将从 for 循环开始的部分替换为:

JSONArray root = new JSONArray();
for (Entry<String, List<String>> entry : directionMap.entrySet()) {
JSONObject stop = new JSONObject();
String stopName = entry.getKey();
stop.put("stop_name", stopName);

JSONObject arrivalTime = new JSONObject();
JSONArray monFriArrivalTime = new JSONArray();
JSONArray satArrivalTime = new JSONArray();
JSONArray sunFriArrivalTime = new JSONArray();
for (String str: entry.getValue()) {
monFriArrivalTime.add(str);
}
for (String str: directionMap2.get(stopName)) {
satArrivalTime.add(str);
}
for (String str: directionMap3.get(stopName)) {
sunFriArrivalTime.add(str);
}
arrivalTime.put("mon-fri", monFriArrivalTime);
arrivalTime.put("sat", satArrivalTime);
arrivalTime.put("sun", sunFriArrivalTime);
stop.put("arrival_time", arrivalTime);
root.add(e);
}
System.out.println(root);

关于java - 循环3个map获取同一个key的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30801117/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com