• Post Reply Bookmark Topic Watch Topic
  • New Topic

Loop through 3 maps to get the values of the same key  RSS feed

 
Alex Ardoin
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am trying to put all values in the 3 maps directionMap. directionMap2, directionMap3 in the JSONArray stop. In these maps all keys name (stop_name) are the the same but the values timeEntries, timeEntries2, timeEntries3 (mon-fri, sat, son) are different so I want to put all these values in the same JSONObject arrivals but I facing problem to loop through all these 3 maps to get all values of the same key together?

I have tried to put the 3 for loops them in each other but it was not the right solution.

directionMap contains the map from stop name to the Mon-Fri arrival times, directionMap2 contains the map from stop name to the Sat arrival times and directionMap3 contains the map from stop name to the Sun arrival times and all of them have the same number of entries.

I appreciate any help.


The result should look as this simple:
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My guess is that you need to do the same thing for one map as you do the other two. Try solving the problem for one map and putting this into a method, then calling the method for the three maps.
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to regroup your data first. Currently your maps are schedule/stop/time, and you want to convert them to stop/schedule/time. To make this task much easier, I suggest you use strong types instead of String. Remember: Strings Are Bad.

Implement a class Stop:



Then write a method that takes your input data in the form of Strings, and converts them to a List<Stop>. Finally, converting from a Stop to JSON is pretty easy.
 
Alex Ardoin
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This code here works for me.

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!