• 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

Redesigning dictionaries of Hashpmap

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I am part of a project where we are facing a serious problem of restructuring the existing dictionaries of Hashmap. The code looks like :

Map<String, Map<String, String>> level1 = new HashMap<String, Map<String, String>>();
Map<String, Map<String, Map<String, String>>> level2 = new HashMap<String, Map<String, Map<String, String>>>();

This is used for generating reports and there are certain condition on this maps to check if XXX is present in these dictionaries then add that column/part on report.

Client wants to get rid of these dictionaries of Hashmap, basically they are looking for a good design.

I tried to provide a wrapper class around these structure like:

class Level1 extends Map<String, String>{}
class Level2 extends Map<String, Level1>{}

with this code looks good and and there is no need to change other part of existing code, but we are looking for some better solution.

If anyone can suggest better approach or design would be great help.


Thank you in advance.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What exactly is not good in your opinion about your own idea with the Level1 and Level2 classes?
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What exactly is Map<String, String> in Level1? Is it instance of a domain class with attributes? or just objects with collection of different attributes? Going a bit deeper into the contents of Level1 can help you replace the Map<String,String> with domain classes. Now again with the domain classes if there are multiple of them, you can come up with IS-A or HAS-A relationship among the domain classes and end up with something like Map<String, MyDomainClass> level1 = new HashMap<String, MyDomainClass>()

Based on this you can also refine the Level2.
 
Priya Joshi
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your replies.

Basically I am looking if somehow I can reduce the dictionaries of Map i.e. map inside map

Map<String, Map<String, String>> level1 = new HashMap<String, Map<String, String>>();
Map<String, Map<String, Map<String, String>>> level2 = new HashMap<String, Map<String, Map<String, String>>>();

this is how the current implementation had designed the data structure of Maps.

Any better substitute of Map?
 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hashmaps retrieve data in O(1) time provided you have the key you are looking for. So its a better performing data structure.

If you are using Hashmaps to check for existence of some value and not really using its key-value nature then you can even use an indexed based array where the value at any index would indicate whether to add the column to the report. But I assume you would be using the value present in the Hashmap to generate the report.

I suggested an approach to reduce the use of Maps by using Domain classes.
 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might also want to look at TreeMap to maintain 2 levels of heirarchy
 
Priya Joshi
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Mohamed for the suggestions, I will try to follow.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic