我有以下代碼:無法從java.lang.Integer中轉換至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(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());
}
我得到以下錯誤:
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>)
我是新來的流,並讀取文檔沒有按爲我清除這件事。任何幫助將不勝感激。
你的代碼很奇怪。你爲什麼使用構造函數來修改靜態值?你爲什麼試圖將'outer'的實例映射到'inner'的靜態實例?你期望它產生什麼? – shmosel
@shmosel這只是我爲了解流API的工作而編寫的測試代碼。 – goelakash