I'm new to generics, so not sure where I'm going wrong...
I have classes, called Cat, Dog and Rabbit, which implement the interface Animal.
The following code will compile
Set<? extends Animal> animalSet;
Set<Dog> dogSet = new HashSet<Dog>();
animalSet = dogSet;
But the following code will not
Map<String, Set<? extends Animal>> animalMap;
Map<String, Set<Dog>> dogMap = new HashMap<String, Set<Dog>>();
animalMap = dogMap; // this line will not compile
The compiler says the types are incompatible. Where am I going wrong?
UPDATE
Thanks for everyone's help
I've changed the first line of code by adding another wildcard The following code will compile
Map<String, ? extends Set<? extends Animal>> animalMap;
Map<String, Set<Dog>> dogMap = new HashMap<String, Set<Dog>>();
animalMap = dogMap;
See also the solution given by Cyrille Ka below - use putAll() to transfer values from dogMap to animalMap, instead of assigning dogMap to animalMap.