Bug 72857

Summary: 1.5 generics don't fully support polymorphism
Product: [Eclipse Project] JDT Reporter: Alan Malloy <alan>
Component: CoreAssignee: JDT-Core-Inbox <jdt-core-inbox>
Status: RESOLVED WORKSFORME QA Contact:
Severity: normal    
Priority: P3    
Version: 3.0   
Target Milestone: 3.1 M2   
Hardware: PC   
OS: Windows XP   
Whiteboard:

Description Alan Malloy CLA 2004-08-30 03:17:04 EDT
There appears to be a problem with declaring a variable of some generic type 
and initializing it to a subclass of that type, which should work fine. (Note 
that this may be a duplicate of bug #72754) Below is an example:

Map<String, Set<String>> m = new TreeMap<String, TreeSet<String>>();
Then faced with this code, the Eclipse compiler complains: "Type mismatch: 
cannot convert from TreeMap<String,TreeSet<String>> to 
Map<String,Set<String>>".

I suspect that this may be a duplicate of bug #72754 because a declaration 
without nested generics, such as Map<String, String>, works just fine. But 
since it may not be (this has to do with variable assignment, not method 
invocation), I felt I should bring it to your attention. Thank you.
Comment 1 Kent Johnson CLA 2004-08-30 11:22:37 EDT
You cannot pass in a TreeSet since it expects a Set. Read up on subtypes & 
generics.

java version "1.5.0-rc", build 1.5.0-rc-b63 complains with:

test.java:5: incompatible types
found   : 
java.util.TreeMap<java.lang.String,java.util.TreeSet<java.lang.String>
>
required: java.util.Map<java.lang.String,java.util.Set<java.lang.String>>
                Map<String, Set<String>> m = new TreeMap<String, 
TreeSet<String>
>();
                                             ^
1 error
Comment 2 John Maraist CLA 2004-12-03 12:25:30 EST
A workaround for the original poster would be to declare 
 
Map<String, ? extends Set<String>> m