带有通配符和静态包装器的Java泛型

Ged*_*rox 5 java generics

请问关于如此通用的问题标题,但不确定如何正确地调用我的问题.

查看默认的Collections.singleton调用结果类型:

Set<Number> numberSingleton = 
    Collections.singleton((Number) null);

Set<Collection> rawCollectionSingleton = 
    Collections.singleton((Collection) null);

Set<Collection<String>> stringCollectionSingleton = 
    Collections.singleton((Collection<String>) null);

Set<? extends Collection<?>> anyCollectionSingleton = 
    Collections.singleton((Collection<?>) null);
Run Code Online (Sandbox Code Playgroud)

我无法解释的是最后一行.为什么? extends Collection<?>用而不是简单Collection<?>

这是正确的解决方法吗?

Set<Collection<?>> anyCollectionSingleton = 
    Collections.<Collection<?>>singleton((Collection<?>) null);
Run Code Online (Sandbox Code Playgroud)

为什么我开始这一切:

我有一个没有编译行的问题:

java.util.Optional.ofNullable((Collection<?>) a).orElse((Collection<?>) b);
Run Code Online (Sandbox Code Playgroud)

这似乎解决了这个问题,但价格是多少?

java.util.Optional.<Collection<?>>ofNullable((Collection<?>) a).orElse((Collection<?>) b);
Run Code Online (Sandbox Code Playgroud)

tho*_*ais 0

不,这是一个完全不同的概念。

与什么一起使用

Set<? extends Collection<?>> anyCollectionSingleton = 
Collections.singleton((Collection<?>) null);
Run Code Online (Sandbox Code Playgroud)

是你有一个扩展泛型类集合的泛型类。

所以,这是关于继承的。