Java 5, Generic, Map and wildcards

  • Thread starter Benjamin Lerman
  • Start date
B

Benjamin Lerman

Hi all,

I've got a weird problem with generics and maps.

I have the 2 following lines :

Collection<? extends ParametresGeneraux> l = new TreeSet<ParametresGlobaux>();

Map<String,Collection<? extends ParametresGeneraux> > m = new TreeMap<String, TreeSet<ParametresGlobaux>>();

The first one causes no problem, the second one causes this error:

Type mismatch: cannot convert from TreeMap<String,TreeSet<ParametresGlobaux>> to Map<String,Collection<? extends ParametresGeneraux>>

Knowing that ParametresGlobaux extends ParametresGeneraux and TreeSet
implements Collection, I don't really see what is wrong.

Is this a misfeature of Generics, or is there any way to have this work
(other than recreating my map).

Thanks.
 
H

Hendrik Maryns

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
NotDashEscaped: You need GnuPG to verify this message

Benjamin Lerman schreef:
Hi all,

I've got a weird problem with generics and maps.

I have the 2 following lines :

Collection<? extends ParametresGeneraux> l = new TreeSet<ParametresGlobaux>();

Map<String,Collection<? extends ParametresGeneraux> > m = new TreeMap<String, TreeSet<ParametresGlobaux>>();

Multilevel generics with wildcards are hard to understand. You want

Map<String,? extends Collection<? extends ParametresGeneraux> > m = new
TreeMap said:
The first one causes no problem, the second one causes this error:

Type mismatch: cannot convert from TreeMap<String,TreeSet<ParametresGlobaux>> to Map<String,Collection<? extends ParametresGeneraux>>

Knowing that ParametresGlobaux extends ParametresGeneraux and TreeSet
implements Collection, I don't really see what is wrong.

Is this a misfeature of Generics, or is there any way to have this work
(other than recreating my map).

See
http://www.angelikalanger.com/Gener...ments.html#What do multilevel wildcards mean?
for an excellent explanation of the matter. (or
http://tinyurl.com/h3j8y if your newsreader messes up).

HTH, H.

--
Hendrik Maryns

==================
www.lieverleven.be
http://aouw.org
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFEECewe+7xMGD3itQRAqonAJwMC1MG7tPO6jopoqi3Fs0cfMUpJQCfZqY4
mxMGLeeDmI2MmkNgzl0FQBs=
=GpSV
-----END PGP SIGNATURE-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,522
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top