Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Java
  3. Question
Java

Explain java map of maps.

Asked by Ian Robertson Oct 4, 2022 1.5K views 1 answer
Share

About this question

in the project I am currently working on we had three different types of prices depending on the age of the user (adult, child, etc...). So we had on the DB a table looking like this:

PRICES

type     Amount

A         20

B         15

C         ..

D         ..

At first we only had 4 different types of prices, so in the code, we had something like this:

Map prices = new HashMap();

Where the keys were the price type.

Recently, they added a new business rule that adds 3 subtypes to every price type, so now we have something like this:

PRICES

type   subtype  Amount

A          1      20

A          2      15

A          3      ..

B          1      ..

B          2      ..

...        ..     ..

Which of the following two options do you think is better and why?

Nested Maps

Map> prices;

where the keys are the price type and subtype:

prices.get(type).get(subtype);

Combined Keys

The same map than originally:

Map prices;

And concatenate the keys to index the different prices:

prices.get(type+"_"+subtype);

Your answer

1 Answer

More Java discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Java Blogs

Guides, tips and career advice on Java from JanBask experts.