Click here to Skip to main content
Rate this: bad
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 2:18am
Edited 17-Jan-13 2:22am
Sergey Alexandrovich Kryukov at 17-Jan-13 7:19am
What is "non-dynamic" creation then? :-)
Ajit Kumar Nayak at 17-Jan-13 7:24am
I mean ,I need the Hash Map at Run time .... :-)
Sergey Alexandrovich Kryukov at 17-Jan-13 7:37am
Do you think it can work before run time somehow?
Ajit Kumar Nayak at 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 at 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?

1 solution

Rate this: bad
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.

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

  Print Answers RSS
0 Peter Leow 338
1 Kornfeld Eliyahu Peter 230
2 OriginalGriff 223
3 Maciej Los 210
4 Sergey Alexandrovich Kryukov 152
0 OriginalGriff 383
1 Peter Leow 318
2 Kornfeld Eliyahu Peter 260
3 Maciej Los 230
4 Sergey Alexandrovich Kryukov 212

Advertise | Privacy | Mobile
Web02 | 2.8.150301.1 | Last Updated 17 Jan 2013
Copyright © CodeProject, 1999-2015
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