Get values for a key in multiple TreeMaps

Dear all,

I have a number of TreeMaps <Long, Integer> each containing values for a specific dataset.

Map<Long, Integer> Heating = new TreeMap<>();
Map <Long, Integer> Cooling = new TreeMap<>();
Map <Long, Integer> Appliances = new TreeMap<>();
etc.

I am now trying to write out the values to a file line by line.  

Usually I iterate through a map in the following way but not sure how to get each value for the same long (Date/TIme) into a string, i.e. Date key , Heating value, Cooling value, Appliances value.

Thanks

Iterator<Map.Entry<Long, Integer>> entries = map.entrySet().iterator();
while (entries.hasNext()) {
    Map.Entry<Long, Integer> entry = entries.next();
    System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
}

Open in new window

AndyC1000Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mccarlIT Business Systems Analyst / Software DeveloperCommented:
Quick and Dirty solution...

Just access the other maps by the key that you are currently iterating, ie. if you are iterating over the Heating map, the Heating value is retrieved easily as entry.getValue(), the other values can be retrieved by Cooling.get(entry.getKey()) and Appliances.get(entry.getKey())


Recommended solution...

Refactor your code so that you have just one map and the values of the map are objects that contain all data that you need for that same long (Date/Time). ie. you might create a class called "Values" that simply hold one value for Heating, one for Cooling, one for Appliances (the specifics of this class, ie. whether it is immutable or not, etc will depend on how the data in these maps gets populated).

Then you can just have the one TreeMap, such as.... Map<Long, Values> map = new TreeMap<>();

Then when you iterate over that map, you cant print out, entry.getKey(), entry.getValue().getHeating(), entry.getValue().getCooling(), and entry.getValue().getAppliances()
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.