[英]Why Iterator<Entry<Integer, Double> cannot be converted to Iterator<Entry<? extends Integer, ? extends Double>>
I have the sample code below attempting to construct an immutable map out of an iterable:我有下面的示例代码,试图从一个可迭代对象中构建一个不可变的映射:
ImmutableList<Entry<Integer, Double>> list = ImmutableList.of(
new AbstractMap.SimpleEntry<Integer, Double>(4, 2d),
new AbstractMap.SimpleEntry<Integer, Double>(16, 4d));
ImmutableMap.<Integer, Double>builder().putAll(list); // No error
ImmutableMap.<Integer, Double>builder().putAll(() -> list.stream().iterator()); // Error - cannot convert type...
I am getting the error Bad return type in lambda expression: Iterator<Entry<Integer, Double>> cannot be converted to Iterator<Entry<? extends Integer, ? extends Double>>
我Bad return type in lambda expression: Iterator<Entry<Integer, Double>> cannot be converted to Iterator<Entry<? extends Integer, ? extends Double>>
收到错误Bad return type in lambda expression: Iterator<Entry<Integer, Double>> cannot be converted to Iterator<Entry<? extends Integer, ? extends Double>>
错误Bad return type in lambda expression: Iterator<Entry<Integer, Double>> cannot be converted to Iterator<Entry<? extends Integer, ? extends Double>>
Bad return type in lambda expression: Iterator<Entry<Integer, Double>> cannot be converted to Iterator<Entry<? extends Integer, ? extends Double>>
Bad return type in lambda expression: Iterator<Entry<Integer, Double>> cannot be converted to Iterator<Entry<? extends Integer, ? extends Double>>
. Bad return type in lambda expression: Iterator<Entry<Integer, Double>> cannot be converted to Iterator<Entry<? extends Integer, ? extends Double>>
。 From my understanding this should be valid because Integer is a valid upper bound of Integer, and Double is a valid upper bound of Double.根据我的理解,这应该是有效的,因为 Integer 是 Integer 的有效上限,而 Double 是 Double 的有效上限。 What is the problem here and how can I fix specifically the iterator code?这里有什么问题,我该如何修复迭代器代码?
I need to fix the iterator code in particular since I am trying to transform another collection into a map like below.我需要特别修复迭代器代码,因为我正在尝试将另一个集合转换为如下所示的地图。 I am using guava on android so do not have access to the default guava collect() methods (though I think I can implement them manually if there is really no other way to do this).我在 android 上使用 guava,所以无法访问默认的 guava collect() 方法(尽管我认为我可以手动实现它们,如果真的没有其他方法可以做到这一点)。
ImmutableList<Integer> numberList = ImmutableList.of(2,4,8,12,16,24,32);
ImmutableMap.<Integer, Double>builder().putAll(() ->
numberList.stream().map(num ->
(Entry<Integer, Double>) new AbstractMap.SimpleEntry<Integer, Double>(num, Math.sqrt(num))).iterator());
Oh boy, this one is ugly.哦,男孩,这个丑陋的。
You're right that Entry<? extends Integer, ? extends Double>
你是对的那个Entry<? extends Integer, ? extends Double>
Entry<? extends Integer, ? extends Double>
Entry<? extends Integer, ? extends Double>
is a "subtype" of Entry<Integer, Double>
(I think it's not technically called a subtype, but is effectively one). Entry<? extends Integer, ? extends Double>
是Entry<Integer, Double>
的“子类型”(我认为它在技术上不被称为子类型,但实际上是一个子类型)。 But, as you may know, Iterator<Subtype>
is not a subtype of Iterator<Supertype>
.但是,您可能知道, Iterator<Subtype>
不是Iterator<Supertype>
的子类型。 Instead you have to do Iterator<? extends Subtype>
相反,您必须执行Iterator<? extends Subtype>
Iterator<? extends Subtype>
. Iterator<? extends Subtype>
。
So you'd need to add just one more ? extends
所以你只需要再添加一个? extends
? extends
in there:在那里? extends
:
Iterator<? extends Entry<? extends Integer, ? extends Double>>
^^^^^^^^^
I'm not sure offhand what the best way to do that is, though.不过,我不确定最好的方法是什么。
Integer is not a valid upper bound of Integer, you can't extend class A from class A. For example yout can't write this: Public class Actor extends Actor{ } So to solve your problem make Number instead Integer like this Iterator<Entry<? Integer 不是 Integer 的有效上限,您不能从 A 类扩展 A 类。例如,您不能这样写: Public class Actor extends Actor{ } 所以要解决您的问题,请使用 Number 代替 Integer 像这样 Iterator<条目<? extends Number, ?extends Number>>扩展数, ?扩展数>>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.