• Post Reply Bookmark Topic Watch Topic
  • New Topic

question in generic Map  RSS feed

 
vasu chowdary
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
 
Pankaja Shinde
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Code is working fine for me.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor 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
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor 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
 
Scott Shipp
Ranch Hand
Posts: 224
12
Eclipse IDE IntelliJ IDE Java Scala 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!