Click here to Skip to main content
12,506,574 members (54,913 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: Java
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
Comments
   
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

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

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.
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160927.1 | Last Updated 17 Jan 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100