Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Map within a List

 
Niall Loughnane
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or:
 
Rob Spoor
Sheriff
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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>.
 
Niall Loughnane
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sound thanks a million for your replies
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic