有没有一种类型安全的方法来实现以下内容?
public void myMethod( Map<String, ? extends List<String>> map )
{
map.put("foo", Collections.singletonList("bar");
}
上述实现是无法工作的。它需要一个Map<String, ? super List<String>>
才能正确编译方法map.put()
。但是我的方法不会以这种方式接受List的任何子类型。因此,我必须使用Map<String, ? extends List<String>>
。如何以类型安全的方式解决这个问题?