を定義し、次のスニペットを考えてみましょう:利用ラムダ式
public class JavaApplication4 {
static <T> List<T> functionConcat(List<T> l1, List<T> l2) {
return Stream.concat(l1.stream(), l2.stream()).collect(Collectors.toList());
}
// functionConcat in lambda form
static final BinaryOperator<List<? extends Number>> lambdaConcat = (l1, l2)
-> Stream.concat(l1.stream(), l2.stream()).collect(Collectors.toList());
public static void main(String[] args) {
// DOES NOT WORK with lambdaConcat
final List<Integer> x = new LinkedList<List<Integer>>()
.stream().reduce(new LinkedList<>(), lambdaConcat);
final List<Double> y = new LinkedList<List<Double>>()
.stream().reduce(new LinkedList<>(), lambdaConcat);
// WORKS with functionConcat
final List<Integer> x2 = new LinkedList<List<Integer>>()
.stream().reduce(new LinkedList<>(), JavaApplication4::functionConcat);
final List<Double> y2 = new LinkedList<List<Double>>()
.stream().reduce(new LinkedList<>(), JavaApplication4::functionConcat);
}
}
はlambdaConcat
なmain()
で対応する二つの文が正しいとなっていることを解決する方法はありますか?
私はBinaryOperator<List<Number>>
、BinaryOperator<List<?>>
、BinaryOperator<List<? extends Number>>
、とさえBinaryOperator<List>
ようなタイプを表現しようとしたが、、当然のことながら、それらのどれも動作しません。理想的には、私は私がfunctionConcat
で行ったように型パラメータ<T>
でlambdaConcat
を書きたいと思いますが、私はラムダ式でそれを表現する方法を発見していません。
は、あなたの究極の結論に反対するまでもありませんが、 'BinaryOperatorは 'T'は、入力と出力の両方に使用されているので、'不変である必要があります。そして、出力のために、 '? Numberは十分に正確ではない。私たちは、あなたが示してきたように 'T'が 'Integer'または' Double'する必要があります。オリジナルの 'lambdaConcat'はランク2多型の試みであると思われることに注意してください!実際 –
@JohnVasileff、: 'BinaryOperator'は不変である必要があります –