Nested Hashmaps best practices?

I find myself using nested Dictionaries and LinkedHashMaps fairly often. It has me thinking, is this good practice or should I be using some other data structure. Often times my maps are 3D in nature.

LinkedHashMap<String, LinkedHashMap<String, LinkedHashMap<String, String>>> mapOfOIDS = new LinkedHashMap<String, LinkedHashMap<String, LinkedHashMap<String, String>>>

It can often be messy to work with at times and was wondering if there was a better approach? For this instance I want to model a number of unique hostname, who have a list of unique port number, who have a list of unique OIDS.

Answer

You can actually combine the three String keys into one using some kind of delimiter. Let the current data as per your implementation be { "abc" :{ "qwe" : {"qaz" : "MYDATA"}}} Here you could combine the three keys into one as abc~qwe~qaz. So the resultant map would be {"abc~qwe~qaz" : "MYDATA"}

Leave a Reply

Your email address will not be published. Required fields are marked *