This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Map within a List" Watch "Map within a List" New topic

Map within a List

Niall Loughnane
Ranch Hand

Joined: Dec 07, 2006
Posts: 208

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?

Matter Al-Otaibi

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

Joined: Jan 30, 2000
Posts: 18671

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

Joined: Oct 27, 2005
Posts: 19651

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

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
I agree. Here's the link:
subject: Map within a List
Similar Threads
question from nikos blog(generics)
generics and maps
iteration and writing to a file
Converting Hash map to linked list