Answer the question
In order to leave comments, you need to log in
How to properly use lambda expressions in Java?
Good evening everyone! There is some code that uses lambda expressions.
public class Calculator {
interface IntegerMath {
int operation(int a, int b);
}
int operateBinary(int a, int b, IntegerMath op) {
return op.operation(a, b);
}
public static void main(String... args) {
Calculator myApp = new Calculator();
IntegerMath addition = (a, b) -> a + b;
IntegerMath subtraction = (a, b) -> a - b;
System.out.println("40 + 2 = "
+ myApp.operateBinary(40, 2, addition));
System.out.println("20 - 10 = "
+ myApp.operateBinary(20, 10, subtraction));
}
}
public class Solution2 {
interface RandomDouble {
double operation(double[] numbers);
}
public double operateBinary(double[] numbers, RandomDouble op) {
return op.operation(numbers);
}
public static void main(String[] args) {
Solution2 myApp = new Solution2();
// RandomDouble sum
// RandomDouble max
// RandomDouble min
System.out.println("Sum: " + myApp.operateBinary(numbers, sum));
System.out.println("Min: " + myApp.operateBinary(numbers, min));
System.out.println("Max: " + myApp.operateBinary(numbers, max));
}
}
Answer the question
In order to leave comments, you need to log in
Just don't understand the syntax?
public static void main(String[] args) {
Solution2 myApp = new Solution2();
double[] numbers = new double[10];
RandomDouble sum = (arr) -> { double forSum = arr[0]; return forSum; };
RandomDouble min = (arr) -> { double forMin = arr[0]; return forMin; };
RandomDouble max = (arr) -> { double forMax = arr[0]; return forMax; };
System.out.println("Sum: " + myApp.operateBinary(numbers, sum));
System.out.println("Min: " + myApp.operateBinary(numbers, min));
System.out.println("Max: " + myApp.operateBinary(numbers, max));
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question