aspose file tools*
The moose likes Java in General and the fly likes Map within a List Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Map within a List" Watch "Map within a List" New topic
Author

Map within a List

Niall Loughnane
Ranch Hand

Joined: Dec 07, 2006
Posts: 208
Hiya,

i want to get a linkedhashmap within a linkedlist but it wont work :-(

i have tried

List<Map<String, String>> list = new LinkedList<HashMap<String, String>> ();

whats the best way?

cheers
Matter Al-Otaibi
Greenhorn

Joined: Oct 12, 2007
Posts: 1
Hi Nail,

This is my first reply in this forum and I hope it be beneficial.

I think it is not possible to apply polymorphism on the Type of the generic collection; the generic Type has to be the same. so this I think it will work
List<HashMap<String, String>> list = new LinkedList<HashMap<String, String>>
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Or:


"I'm not back." - Bill Harding, Twister
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19762
    
  20

As Jim said, just use Map<String,String> on the right side as well.

The declaration does not care about which type of map you use, all it cares about is that you want to add Map<String,String> objects. Now why should your initialization be more restrictive than that?
If you really need it to be restricted to HashMap<String,String> you should also declare it as HashMap<String,String>.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Niall Loughnane
Ranch Hand

Joined: Dec 07, 2006
Posts: 208
sound thanks a million for your replies
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Map within a List