简体   繁体   English

列表<? super List<? super Integer> &gt; 和列表<? extends List<? super Integer> &gt; 以及如何正确使用它?

[英]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
  • Here I am trying to understand how to interpret these two statements在这里,我试图了解如何解释这两个陈述
    • Expression:1表达式:1
      • Here we say that the List on the RHS must be such that all the elements of the list will satisfy the condition ? super List<? super Integer>这里我们说 RHS 上的 List 必须是列表中的所有元素都满足条件? super List<? super Integer> ? super List<? super Integer>
      • but 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> .
      • Still why are we not getting compilation error here?仍然为什么我们在这里没有收到编译错误?
    • Expression:2表达式:2
      • Here we expect that the elements on the RHS must be the subtype of List<? super Integer>这里我们期望 RHS 上的元素必须是subtype of List<? super Integer> subtype of List<? super Integer>
      • so doubleList intuitively can be seen as a candidate that can satisfy the condition.所以doubleList直观上可以看成是满足条件的候选。
      • Why still I am getting compilation error if I include 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?包含doubleListintegerListnumberList的列表可以这样做吗? 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM