[英]List<? super List<? super Integer>> and List<? extends List<? super Integer>> and how to use it correctly?
Consider the following snippet:考虑以下片段:
List<Double> doubleList = null;
List<Integer> integerList = null;
List<Number> numberList = null;
//expression:1
List<? super List<? super Integer>> superDoubleList = Arrays.asList(doubleList, integerList,numberList);
//expression:2
//here doubleList will cause compilation error
List<? extends List<? super Integer>> extendsDoubleList = Arrays.asList(integerList,numberList);//doubleList
? super List<? super Integer>
这里我们说 RHS 上的 List 必须是列表中的所有元素都满足条件? super List<? super Integer>
? super List<? super Integer>
doubleList
/ integerList
/ numberList
are not satisfying this condition anyhow - as we expect a type that is a supertype of List<? super Integer>
但是doubleList
/ integerList
/ numberList
无论如何都不满足这个条件——因为我们期望一个类型是List<? super Integer>
List<? super Integer>
. List<? super Integer>
.subtype of List<? super Integer>
这里我们期望 RHS 上的元素必须是subtype of List<? super Integer>
subtype of List<? super Integer>
doubleList
intuitively can be seen as a candidate that can satisfy the condition.所以doubleList
直观上可以看成是满足条件的候选。doubleList
in the Arrays.asList
expression?如果我在Arrays.asList
表达式中包含doubleList
,为什么仍然会出现编译错误? . .Not sure if I am interpreting the expressions in the right way - and what is wrong possibly that logically it does not seem to fit the explanation I gave above?不确定我是否以正确的方式解释这些表达式 - 从逻辑上讲,它似乎不符合我上面给出的解释,这可能有什么问题?
The two cases that compiles, compiles because the type inference algorithm tries its best to infer the type parameter for the asList
call to make your code compile.编译的两种情况都会编译,因为类型推断算法会尽力推断asList
调用的类型参数,以使您的代码编译。 It's not about the types of the three lists (they're only indirectly related).这与三个列表的类型无关(它们只是间接相关)。 It's all about the type that Arrays.asList
returns.这都是关于Arrays.asList
返回的类型。
In the first case:在第一种情况下:
List<? super List<? super Integer>> superDoubleList = Arrays.asList(doubleList, integerList,numberList);
To make your code compile, Arrays.asList
, just has to create a List<List<?>>
.为了让你的代码编译, Arrays.asList
,只需要创建一个List<List<?>>
。 After all, the three lists are all "lists of something ", so that is possible.毕竟这三个列表都是“某物的列表”,所以这是可能的。
And List<List<?>>
is a kind of List<? super List<? super Integer>>
而List<List<?>>
是一种List<? super List<? super Integer>>
List<? super List<? super Integer>>
List<? super List<? super Integer>>
. List<? super List<? super Integer>>
. This is because List<?>
is a super type of List<? super Integer>
这是因为List<?>
是List<? super Integer>
List<? super Integer>
- "a list of some Integer
supertype" is a kind of "a list of some objects". List<? super Integer>
- “一些Integer
超类型的列表”是一种“一些对象的列表”。
Another interpretation of this, is to think of ? super T
对此的另一种解释,是想到? super T
? super T
as "consumer of T
" and ? extends T
? super T
为“消费T
”的? extends T
? extends T
as "producer of T
". ? extends T
为“ T
生产者”。 ( PECS ) In this interpretation, List<? super List<? super Integer>>
( PECS ) 在这种解释中, List<? super List<? super Integer>>
List<? super List<? super Integer>>
List<? super List<? super Integer>>
means "a list that can consume lists that can consume integers". List<? super List<? super Integer>>
表示“可以使用可以使用整数的列表的列表”。 "Consume" in the context of lists just means "add".列表上下文中的“消费”仅表示“添加”。 Can a list containing doubleList
, integerList
and numberList
do that?包含doubleList
、 integerList
和numberList
的列表可以这样做吗? Sure, it doesn't matter what the contents of the list are, you can always add another List<? super Integer>
当然,列表的内容是什么并不重要,你总是可以添加另一个List<? super Integer>
List<? super Integer>
to the list. List<? super Integer>
到列表中。 It's just that the type of the list has to be List<List<?>>
.只是列表的类型必须是List<List<?>>
。 Even this works:即使这有效:
List<? super List<? super Integer>> superDoubleList =
Arrays.asList(new ArrayList<String>(), new ArrayList<LocalDate>());
Using the same interpretation, List<? extends List<? super Integer>>
使用相同的解释, List<? extends List<? super Integer>>
List<? extends List<? super Integer>>
List<? extends List<? super Integer>>
means "a list that can produce lists that consume integers". List<? extends List<? super Integer>>
表示“可以生成消耗整数的列表的列表”。 Can能
Arrays.asList(integerList,numberList)
do that?去做? Yes, both of those inner lists can consume integers, so the outer list can "produce lists that consume integers", or in other words, a producer of such lists.是的,这两个内部列表都可以使用整数,因此外部列表可以“生成使用整数的列表”,或者换句话说,是此类列表的生产者。
What about this list of lists?这份名单怎么样?
Arrays.asList(doubleList,integerList,numberList)
Is it a producer of lists that can consume integers?它是可以使用整数的列表的生产者吗? Well, no, because doubleList
does not consume integers, but it can produce that.嗯,不,因为doubleList
不消耗整数,但它可以产生整数。
You might be wondering what is the type that the Java compiler has inferred for asList
in this case:在这种情况下,您可能想知道 Java 编译器为asList
推断的类型是什么:
List<? extends List<? super Integer>> extendsDoubleList = Arrays.asList(integerList,numberList);
asList
could create a List<List<? super Integer>>
asList
可以创建一个List<List<? super Integer>>
List<List<? super Integer>>
. List<List<? super Integer>>
. However, the actual inferred type seems to be something else, that can't be expressed in Java's syntax.然而,实际推断的类型似乎是其他东西,不能用 Java 的语法表达。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.