Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to declare HashMap in HashMap

 
Sujith Mahesh
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Sujith",
Please check your private messages.
-DOM
 
ramya narayanan
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just the way you have written it. What exactly is your doubt here?
 
Campbell Ritchie
Sheriff
Pie
Posts: 48967
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Better to declare the Map as type Map<String, Map<Foo>>.
 
Rob Spoor
Sheriff
Pie
Posts: 20529
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sujith Mahesh
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob,

I will declare like this


and While accessing the object I will cast it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic