I have sample code which uses Java 8 new stream functionality (get a range of int values 1 .. 20, skip the first 9, then take remaining 10, each int value: reduce by one and multiply by 2).
System.out.println(Arrays.toString(
IntStream.rangeClosed(1, 20).skip(9).limit(10).map((new IntUnaryOperator() {
@Override
public int applyAsInt(int operand) {
return operand - 1;
}
}).andThen(new IntUnaryOperator() {
@Override
public int applyAsInt(int operand) {
return operand * 2;
}
})).toArray()));
The output is as follows:
[18, 20, 22, 24, 26, 28, 30, 32, 34, 36]
Now I want to replace anonymous classes with Lambda expressions. The following conversion works fine (the second anonymous class replaced with i -> i * 2
lambda expression) and I get the same output:
System.out.println(Arrays.toString(
IntStream.rangeClosed(1, 20).skip(9).limit(10).map((new IntUnaryOperator() {
@Override
public int applyAsInt(int operand) {
return operand - 1;
}
}).andThen(i -> i * 2)).toArray()));
However, when I replace the first anonymous class with lambda expression:
System.out.println(
Arrays.toString(
IntStream.rangeClosed(1, 20).skip(9).limit(10)
.map((v -> v - 1).andThen(i -> i * 2)).toArray()));
I am not able to compile my code. The error says Operator '-' cannot be applied to '<lambda parameter>', 'int'
Do you know how to combine two lambda expressions with IntUnaryOperator.andThen
?
I know I could use ... .limit(10).map(v -> v - 1).map(i -> i * 2).toArray() ...
which works fine but I would like to find out how to use IntUnaryOperator.andThen
with lambdas (if that possible).
You have to explicitly cast the first lambda expression to IntUnaryOperator. The following code works: