Despite Set<String>
is actually a subtype of Collection<String>
, a Map<String, Set<String>>
is not a subtype of Map<String, Collection<String>>
. In fact, they are completely different types and you can't assign one to the other.
Luckily, the Map
interface defines the putAll
method, which has the following signature:
void putAll(Map<? extends K,? extends V> m)
This means that the putAll
method accepts a map whose keys and values might be of types that are subtypes of its own key and value types, respectively.
So, in your example, you could do as follows:
public class YourClass {
private final Map<String, Collection<String>> courses = new HashMap<>();
public YourClass(Map<String, Set<String>> courses) {
this.courses.putAll(courses);
}
}
You only have to make sure that the courses
attribute has been instantiated before invoking putAll
on it.