File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Comparing analogous objects in several collections Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Comparing analogous objects in several collections" Watch "Comparing analogous objects in several collections" New topic

Comparing analogous objects in several collections

Jane Delphiki

Joined: Oct 28, 2006
Posts: 2

I have coded myself into a corner, and I am no longer certain how to approach this. I am working on a program which reads meteorological data from a flat file. I have given the data the following structure:

Each meteorological station is an object: Station.
A SystemData object holds a HashMap of Station Objects.
Each Station has a collection of MonthData objects.
Each MonthData object has a collection of DayData objects.

For the most part, I simply need to work on the data of a single Station at a time, or for several consecutive Stations and then compare the results, so this structure has not given me any trouble.

In addition, though, I need to do a month-by-month comparison of precipitation (which is an attribute of the DayData objects) and separate the values based on the altitude of each Station (recorded in the Station object).

Logically, I just need to iterate through the collections in some fashion, like so:

The problem is that with this approach I need to pull a number out of a hat in order to make the first *for* loop. I can't say:

...because my HashMaps with MonthData are inside Station objects, not to mention that they could be of variable length (ok, so technically they shouldn't have variable lengths, but I'm not the one pulling together the flat files, I'm just supposed to read from them).

Right now the only roads out of this that I see are incredibly murky and roundabout, looping through the Stations for a single month, doing calculations, looping through again for the next month, etc, as many times as necessary, or looping through the Stations once and calculating data for each of the months, and putting the results in separate variables depending on the month. Or looping through the stations to find out what the largest collection of MonthData objects is and use that to iterate (skipping null references when necessary).

Would someone please help me sort out my thinking so I can find a way out of this mess in a less kludgy way than this? If you require more clues to how I've been thinking about this, I will be happy to provide them. At this point I am not sure what is necessary and what is just confusing.

Kind regards,
[ November 01, 2006: Message edited by: Jane Delphiki ]
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

Hi Jane,

Welcome to JavaRanch!

I don't think the second approach (iterating over stations, accumulating month data) is kludgy at all. Sometimes that's what you have to do. I could go into a long discussion of object-oriented data modeling vs a relational model as represented by database tables; sometimes, as here, a relational model just makes sense. But nonetheless, I wouldn't think of using an auxilliary data structure as a bad thing.

[Jess in Action][AskingGoodQuestions]
Jane Delphiki

Joined: Oct 28, 2006
Posts: 2
Thank you for getting back to me on this!

I will go with the second approach. All the rest of the operations I need to perform are such that an object oriented approach makes sense, there are only a couple of comparisons that need some extra attention.

Thanks for the warm welcome, as well! Nice to drop by a friendly place.
I agree. Here's the link:
subject: Comparing analogous objects in several collections
It's not a secret anymore!