12,506,574 members (54,913 online)
Rate this:
See more:
`I am doing an application which needs dynamic creation of Hash Maps.`
```Map<K,V> parent=new Hash Map<K,V>();

if(Some Condition)
{
//Parent map will be added with another sub map

parent = Map<K,Map<K,V>>();

//    and So on
}
```
`I need only 3 levels Like this`
`parent = Map<K,Map<K,Map<K,V>>>();`
Posted 17-Jan-13 1:18am
Updated 17-Jan-13 1:22am
v2

What is "non-dynamic" creation then? :-)
—SA
Ajit Kumar Nayak 17-Jan-13 7:24am

I mean ,I need the Hash Map at Run time .... :-)

Do you think it can work before run time somehow?
—SA
Ajit Kumar Nayak 17-Jan-13 7:47am

I know that,
Explaining my query

I have one map parent=Map<k,v>

i want when i put some condition

suppose i=1
then the map will be Map<k,map<k,v>>
for i=2
Map<k,map<k,map<k,v>>> like this where K,V has different values
Sergey Alexandrovich Kryukov 17-Jan-13 11:43am

Well, you can have it; what's the problem?
And what are you trying to achieve with that? Let's see.
With the map, you can find the value with the time complexity O(1). In your case, you find a map first, most-inner map in that map, and v in that most-inner map. In your case, v may not be unique, as two identical values of v may be found in different instances of map. And the maps are found using their referential identity, unlike v which can be anything. In what case could it be useful? In which cases you would need to create yes another instance of nested maps? and in which cases you would need to put some object (map or v) in the same map? Why?
—SA

Rate this:

## Solution 1

What exactly do you want to acchive?
This is a 3 dimensional HashMap. Where should that be needed?

You need to be very careful with the Keys and the values - do not mix them up.

But I think there must be a simpler solution.
How about creating an Object that holds the HashMap of 2. and 3. Dimension?
Could be at least less confusing.

Top Experts
Last 24hrsThis month
 OriginalGriff 308 David_Wimbley 199 Karthik Bangalore 191 Maciej Los 155 Richard MacCutchan 128
 OriginalGriff 7,406 ppolymorphe 3,984 Maciej Los 3,751 David_Wimbley 3,106 Karthik Bangalore 3,052