Skip to content

Instantly share code, notes, and snippets.

@brijesh-deb
Last active July 22, 2018 06:26
Show Gist options
  • Save brijesh-deb/88aceff88e112f128965300bb2f097cf to your computer and use it in GitHub Desktop.
Save brijesh-deb/88aceff88e112f128965300bb2f097cf to your computer and use it in GitHub Desktop.
#Java8 #Notes

Lambda Expressions

  • Lambda expressions are anonymous method(methods without names) that implement abstract function of functional interfaces.
  • An interface with single abstract method is called functional interface. Example: Runnable interface with run() method.
  • Arraow Operator
    • Arrow operator divides lambda expression into 2 parts
    • (n) -> n*n;
    • The left side specifies the parameters required by the expression, which could also be empty if no parameters are required.
    • The right side is the lambda body which specifies the actions of the lambda expression. It might be helpful to think about this operator as “becomes”. For example, “n becomes n*n”, or “n becomes n squared”.
          interface NumericTest {
          boolean computeTest(int n); 
        }
    
        public static void main(String args[]) {
          NumericTest isEven = (n) -> (n % 2) == 0;
          NumericTest isNegative = (n) -> (n < 0);
    
          // Output: false
          System.out.println(isEven.computeTest(5));
    
          // Output: true
          System.out.println(isNegative.computeTest(-5));
        }
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment