• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Saloon Keeper
Posts: 15619
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This code here works for me.

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic