i am reading data from a text file and want to store HashMap in another HashMap..
HashMap<string,HashMap<string,value>>
how to store data and retrieve it? any sample code will be appreciated... thank u
i am reading data from a text file and want to store HashMap in another HashMap..
HashMap<string,HashMap<string,value>>
how to store data and retrieve it? any sample code will be appreciated... thank u
HashMap in HashMap will cause problems in readability especially when it goes beyond two levels. I assume that when you read data from a text file you want to categorize the inputs from rows and columns which should be similar to multi-level categories or category within a category. If you can post the sample data and your intention, I could come up with a Custom class example.
The above data structure will help you solve any level of nesting while categorizing data. This example is specific to a store items' classification.
This will solve the same problem using one map (although, this does not directly answer your question) by flattening two nested maps into one big map, using a double-key.
Then just create one map with double-key:
This gives a shorter solution. Performance wise it's probably about the same. Memory performance is probably slightly better (just guessing, though).
Creating two Simple Hashmaps: InnerMap and OuterMap
Populating the HashMaps
Retreiving values from HashMaps
You get something that looks like a 2 dimensions HashMap, so to say. Which means you need 2 String to store a value, and also to retrieve one.
You could, for example write a class to wrap that complexity, like that (untested code):
If you want methods to process more than one data at a time, it's more complicated, but follows the same principles.
Example:
Creating and populating the maps
Storing a map
Retrieving a map and its values