无法从java.lang.Integer转换为R. [英] Cannot convert from java.lang.Integer to R
问题描述
我有以下代码:
class inner {
Integer i;
public Integer getValue(){
return i;
}
public void setValue(Integer i){
this.i = i;
}
}
class outer {
public static inner i1;
outer(整数i){
i1.setValue(i);
}
}
公共类MyClass {
public void main(){
List< Integer> ll = Arrays.asList(new outer(2))。stream()。map(outer.i1 :: getValue).collect(Collectors.toList());
}
我收到以下错误:
必填:功能<?超级对象,?扩展R>找到
:outer.i1 :: getValue
原因:无法推断类型变量R
(参数不匹配;无效方法引用
方法类内部的getValue方法无法应用给定类型
required:无参数
found:Object
reason:实际和形式参数列表的长度不同)
其中R,T是类型变量:
R extends方法< R> map中声明的Object(函数<?super T,?extends R>)
我是溪流的新手,阅读文档并不能解决这个问题。任何帮助将不胜感激。
getValue
是一种方法没有争论。
当您尝试将 getValue
的方法引用传递给 Stream
的 map
方法,您试图将 Stream
的元素传递给 getValue
,但是 getValue
不接受任何参数。
如果您愿意,可以用lambda表达式替换方法引用忽略流的外部
元素:
List< Integer> ll = Arrays.asList(new outer(2))。stream()。map(o - > outer.i1.getValue())。collect(Collectors.toList());
然而,这将导致 NullPointerException
,因为你没有在任何地方初始化 public static inner i1
,所以调用外部
构造函数会抛出该异常。 / p>
如果不知道你想要做什么,很难建议如何解决这个问题。
我不知道知道在外部
类中有一个类型为 inner
的静态成员是否有意义,但如果有意义,你可能应该在静态初始化程序块(或其声明)中初始化它。
你可以,例如,改变
public static inner i1;
到
public static inner i1 = new inner();
这将消除异常。
I have the following code:
class inner {
Integer i;
public Integer getValue() {
return i;
}
public void setValue(Integer i) {
this.i = i;
}
}
class outer {
public static inner i1;
outer(Integer i) {
i1.setValue(i);
}
}
public class MyClass{
public void main() {
List<Integer> ll = Arrays.asList(new outer(2)).stream().map(outer.i1::getValue).collect(Collectors.toList());
}
I get the following error:
required: Function<? super Object,? extends R>
found: outer.i1::getValue
reason: cannot infer type-variable(s) R
(argument mismatch; invalid method reference
method getValue in class inner cannot be applied to given types
required: no arguments
found: Object
reason: actual and formal argument lists differ in length)
where R,T are type-variables:
R extends Object declared in method <R>map(Function<? super T,? extends R>)
I am new to streams, and reading the docs doesn't clear this up for me. Any help will be appreciated.
getValue
is a method that takes no arguments.
When you try to pass a method reference of getValue
to Stream
's map
method, you are trying to pass the Stream
's element to getValue
, but getValue
doesn't take any arguments.
You can replace the method reference with a lambda expression if you wish to ignore the outer
element of the Stream:
List<Integer> ll = Arrays.asList(new outer(2)).stream().map(o -> outer.i1.getValue()).collect(Collectors.toList());
However, this will lead to a NullPointerException
, since you don't initialize public static inner i1
anywhere, so invoking the outer
constructor will throw that exception.
It's hard to suggest how to fix that without knowing what you are trying to do.
I don't know if it makes sense to have a static member of type inner
in your outer
class, but if it makes sense, you should probably initialize it in a static initializer block (or in its declaration).
You could, for example, change
public static inner i1;
to
public static inner i1 = new inner();
which will eliminate the exception.
这篇关于无法从java.lang.Integer转换为R.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!