• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Generic question  RSS feed

 
Ronnie Ho
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all, what's wrong with the following method:

public Map<String, List<? extends Object>> test(String... ticketIDs)
{
return new HashMap<String, List<Integer>>();
}
 
Kaydell Leavitt
Ranch Hand
Posts: 690
Eclipse IDE Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The above code compiles but I get a warning that the parameter ticketIDs is not used.

Kaydell
 
Ronnie Ho
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. I guess my question is since the following is OK

why is this illegal ?
 
Charles Lyons
Author
Ranch Hand
Posts: 836
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Picture this: the type you've identified on the left side can contain a list of any generic type as its value. For example, it's perfectly valid to put a List<String> as the value in a Map<String, List<?>> (note the "extends Object" is implied by the wildcard ?).

However, on the right side, you've got a List<Integer> as the type for the map's value, so you can't put a List<String> in there after all, can you?

There are many such wildcard-related problems which arise in generics, and getting your head around them can be a bit challenging!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!