aspose file tools*
The moose likes Beginning Java and the fly likes How to declare HashMap in HashMap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to declare HashMap in HashMap" Watch "How to declare HashMap in HashMap" New topic
Author

How to declare HashMap in HashMap

Sujith Mahesh
Greenhorn

Joined: Jul 11, 2007
Posts: 6
Hi,

I want to declare a HashMap where key is of the type "String" and value is a "HashMap".
Here is what I want to declare,

How do I declare this in Java 5.0?
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

"Sujith",
Please check your private messages.
-DOM
ramya narayanan
Ranch Hand

Joined: Oct 06, 2008
Posts: 338
I think so it will work

HashMap hm1=new HashMap();
HashMap hm2=new HashMap();
hm2.put("key",hm1);

There is term "map of maps " which describe this functionality.
Regards
rakesh sugirtharaj
Ranch Hand

Joined: Dec 16, 2007
Posts: 151
Just the way you have written it. What exactly is your doubt here?


Cheers!
RSR
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39869
    
  28
Better to declare the Map as type Map<String, Map<Foo>>.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19762
    
  20

The problem in this case can be the length of the overall declaration:

There's not a lot you can do about that though. Either you will have to deal with the length, or lower the constraint in the declaration and cast instead.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Sujith Mahesh
Greenhorn

Joined: Jul 11, 2007
Posts: 6
Thanks Rob,

I will declare like this


and While accessing the object I will cast it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to declare HashMap in HashMap