Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Anonymous class replaced with lambda expressions

Tags:

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'

Compilation error

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).

like image 647
Tom Avatar asked Nov 29 '13 14:11

Tom


People also ask

Is lambda expression An anonymous class?

Note that a lambda expression looks a lot like a method declaration; you can consider lambda expressions as anonymous methods—methods without a name.

Does lambda expression eliminates the need of anonymous class?

Lambda expressions are introduced in Java 8. These are used primarily to define inline implementation of a functional interface, i.e., an interface with a single method only. Lambda expression eliminates the need of anonymous class and gives a very simple yet powerful functional programming capability to Java.

Is lambda in Java an anonymous class?

Java For TestersA lambda expression is a short form for writing an anonymous class. By using a lambda expression, we can declare methods without any name.

Which is better lambda expression or anonymous inner class?

Lambda expression can be used where a class implements a functional interface to reduce the complexity of the code. An inner anonymous class is more powerful as we can use many methods as we want, whereas lambda expression can only be used where an interface has only a single abstract method.


1 Answers

You have to explicitly cast the first lambda expression to IntUnaryOperator. The following code works:

System.out.println(         Arrays.toString(                 IntStream.rangeClosed(1, 20).skip(9).limit(10)                         .map(((IntUnaryOperator) v -> v - 1).andThen(i -> i * 2)).toArray())); 
like image 167
jpvee Avatar answered Oct 02 '22 20:10

jpvee