Traditional style to Lambda style

suggest change

Traditional way

interface MathOperation{
    boolean unaryOperation(int num);
}

public class LambdaTry {
    public static void main(String[] args) {
        MathOperation isEven = new MathOperation() {
            @Override
            public boolean unaryOperation(int num) {
                return num%2 == 0;
            }
        };
        
        System.out.println(isEven.unaryOperation(25));
        System.out.println(isEven.unaryOperation(20));
    }
}

Lambda style

  1. Remove class name and functional interface body.
public class LambdaTry {
    public static void main(String[] args) {
        MathOperation isEven = (int num) -> {
            return num%2 == 0;
        };
        
        System.out.println(isEven.unaryOperation(25));
        System.out.println(isEven.unaryOperation(20));
    }
}
  1. Optional type declaration
MathOperation isEven = (num) -> {
    return num%2 == 0;
};
  1. Optional parenthesis around parameter, if it is single parameter
MathOperation isEven = num -> {
    return num%2 == 0;
};
  1. Optional curly braces, if there is only one line in function body
  2. Optional return keyword, if there is only one line in function body

MathOperation isEven = num -> num%2 == 0;

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Lambda Expressions:
* Traditional style to Lambda style

Table Of Contents
5 Lambda Expressions
8 Arrays
10 Maps
11 Strings
25 JAXB
29 Enums
32 Audio
41 Scanner
63 Logging
75 Lists
78 Sets
89 JAX-WS
96 XJC
98 Process
106 Modules
114 Applets
122 JNDI
139 JavaBean
141 Literals
144 Packages
150 JMX
153 JShell
159 Sockets
167 Enum Map
175 Hashtable
177 SortedMap