File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Nested Map Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Nested Map" Watch "Nested Map" New topic
Author

Nested Map

Aman Singh
Ranch Hand

Joined: Oct 30, 2008
Posts: 57
Hi folks,

I am stuck here with populating a nested HashMap.

the relationship I am looking for is:

Area Code Zone Code Value
001 111 10.34
001 222 11.98
001 333 12.99
. . .
. . .
001 666 15.99
002 111 24.39
002 222 25.69
. . .
. . .
002 666 31.34

and so on.

The above structure can be represented as following:

{001 => {111 => 10.34, 222 => 11.98}, {002 => {111 => 24.39, 222 => 25.69}} so on....

so the final outcome I look for is:

On Area code I can call the Zone code and fetch the corresponding value for that zone code.

The structure I have is like this:

Map<Integer, Map><Integer, Double>> areaPrices = new HashMap<Integer, Map><Integer, Double>>();
Map<Integer, Double> zonePrices = new HashMap<Integer, Double>();

areaPrices HashMap will contain "AREA" as key and zonePrices HashMap as value.

I am trying to populate it from my DB code, but its not giving the expected result.

Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2851
    
  11

It looks like you're on the right track. The first declaration doesn't look quite right, but on preview I see that's probably because the bulletin board formatter is adding an extra bracket. For the populating, I'd expect to see two nested loops, not just one. The inner loop would populate the zone prices, and the outer loop would take care of area prices. Part of the outer would involve instantiating new zonePrice maps and adding them to the areaPrice maps.
Aman Singh
Ranch Hand

Joined: Oct 30, 2008
Posts: 57
Hi Greg,

Thanks for the help.

I quite new to this, so please excuse my silly mistakes

This is what I tried but getting an error on the very last line.

Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2851
    
  11

Hi Aman,

No problem! Try to be more specific with your questions though. You say you get a problem on the last line, but is it a compile problem or a run-time problem? You can even paste in the exact error message.

In this case, I suspect you're getting a compile error, because you said areaPrices would be a Map with an Integer key and a Map<Integer, Double> value. You're trying to store a Double in it instead of the zonePrices Map. Why don't you start with pseudocode, or just a description of how you will populate your maps? If you post that here, I'd be happy to take a look at it and give you suggestions.
Aman Singh
Ranch Hand

Joined: Oct 30, 2008
Posts: 57
Hi Greg,

Thanks a lot for your help. It is solved now
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Nested Map