# How to write a conditional expression in lambda expression in Java?

Java 8Object Oriented ProgrammingProgramming

The conditional operator is used to make conditional expressions in Java. It is also called a Ternary operator because it has three operands such as boolean condition, first expression, and second expression.

We can also write a conditional expression in lambda expression in the below program.

## Example

interface Algebra {
int substraction(int a, int b);
}
public class ConditionalExpressionLambdaTest {
public static void main(String args[]) {
System.out.println("The value is: " + getAlgebra(false).substraction(20, 40));
System.out.println("The value is: " + getAlgebra(true).substraction(40, 10));
}
static Algebra getAlgebra(boolean reverse) {
Algebra alg = reverse ? (a, b) -> a - b : (a, b) -> b - a; // conditional expression
return alg;
}
}

## Output

The value is: 20
The value is: 30
Updated on 11-Jul-2020 07:48:12