Lambda Expressions with One Parameter

Java-Lambda Expressions with One Parameter

Lambda Expressions with One Parameter

When a lambda expression has only one parameter, it is not necessary to surround the parameter name with parentheses when it is specified on the left side of the lambda operator. For example, in the following program, ‘i->i*i’ is also a valid way to write the lambda expression.

Lambda Expressions with One Parameter

Program

Lambda Expressions with One Parameter Private Interface Methodss

Program Source

interface Mathematics {
    
    int powerOf(int i);
}
public class Javaapp {

    public static void main(String[] args) {
        
        Mathematics powerOfTwo = i->i*i;
        int ptwo = powerOfTwo.powerOf(5);
        System.out.println("5^2 : "+ptwo);
        
        Mathematics powerOfThree = (i)->i*i*i;
        int pthree = powerOfThree.powerOf(5);
        System.out.println("5^3 : "+pthree);
    }
}

Leave a Comment