Click here to Skip to main content
15,845,742 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
XML
I am doing an application which needs dynamic creation of Hash Maps.
Java
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
       }
XML
I need only 3 levels Like this

Java
parent = Map<K,Map<K,Map<K,V>>>();
Posted
Updated 17-Jan-13 2:22am
v2
Comments
Sergey Alexandrovich Kryukov 17-Jan-13 7:19am    
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 .... :-)
Sergey Alexandrovich Kryukov 17-Jan-13 7:37am    
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

1 solution

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900