• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

question in generic Map

 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I need a solution in below case, i have three classes Party,Person and Organization , where Person and Organization is child classes of Party. I defined below collection and got one problem




can you tell me why it is giving copilation error? and can't we use use generics for above scenario?


thanks,regards
vasu M
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Code is working fine for me.
 
author
Posts: 23887
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

vasu chowdary wrote:
I need a solution in below case, i have three classes Party,Person and Organization , where Person and Organization is child classes of Party. I defined below collection and got one problem




can you tell me why it is giving copilation error? and can't we use use generics for above scenario?




Agreed. Compiles for me too. Perhaps you can post the compilation error that you are getting?

Henry
 
vasu chowdary
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

ok will tell you the actual scenario



and the error is
1 . The method put(String, Map<String,List<? extends TCRMPartyBObj>>) in the type Map<String,Map<String,List<? extends TCRMPartyBObj>>> is not applicable for the arguments
(String, Map<String,List<XPersonBObjExt>>)

2.The method put(String, Map<String,List<? extends TCRMPartyBObj>>) in the type Map<String,Map<String,List<? extends TCRMPartyBObj>>> is not applicable for the arguments
(String, Map<String,List<XOrganizationBObjExt>>)


in the above TCRMPartyBObj is parent for both XPersonBObjExt and XOrganizationBObjExt.

can you tell me why we are getting above compilation errors?
 
Henry Wong
author
Posts: 23887
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

vasu chowdary wrote:
ok will tell you the actual scenario



and the error is
1 . The method put(String, Map<String,List<? extends TCRMPartyBObj>>) in the type Map<String,Map<String,List<? extends TCRMPartyBObj>>> is not applicable for the arguments
(String, Map<String,List<XPersonBObjExt>>)

2.The method put(String, Map<String,List<? extends TCRMPartyBObj>>) in the type Map<String,Map<String,List<? extends TCRMPartyBObj>>> is not applicable for the arguments
(String, Map<String,List<XOrganizationBObjExt>>)


in the above TCRMPartyBObj is parent for both XPersonBObjExt and XOrganizationBObjExt.

can you tell me why we are getting above compilation errors?




First, let's look at why the first scenario compiles. In this scenario, you are trying to pass a List<? extends Party> instance into a method that takes a List<? extends Party> instance. This is, of course, an exact match and should compile.

In this second scenario, you are trying to pass a Map<String, List<XPersonBObjExt>> instance into a method that takes a Map<String, List<? extends TCRMPartyBObj>> instance. This is *not* a match... but should it work? Well, no. The wildcard actually doesn't come into affect here -- as the wildcard is not for the map.

You have a non-wildcard map instance variable, whose one of its generics is specified as a wildcard. And you are trying to assign it from another non-wildcard map instance variable, with different generics. It is not a match, hence, the method call is not allowed.


To clarify regarding the wildcard, since you will likely point it out... Yes, you can pass a List<XPersonBObjExt> instance into a method that takes a List<? extends TCRMPartyBObj> instance, but that does not mean that you can pass a Map<String, List<XPersonBObjExt>> instance into a method that takes a Map<String, List<? extends TCRMPartyBObj>> instance. The reason stated above.

Henry
 
Ranch Hand
Posts: 239
12
Scala IntelliJ IDE Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry's explanation is great. I have a related post about this on my blog.
 
I would challenge you to a battle of wits, but I see you are unarmed - shakespear. Unarmed tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic