Skip to content

Instantly share code, notes, and snippets.

@ucguy4u
Last active September 19, 2020 06:14
Show Gist options
  • Save ucguy4u/aa97107feb5c63a26c7878855a98610d to your computer and use it in GitHub Desktop.
Save ucguy4u/aa97107feb5c63a26c7878855a98610d to your computer and use it in GitHub Desktop.
Creating a new custom functional interface
package com.ucguy4u.functional.java;
/**
* @author chauhanuday
*/
public class Lambdas_03 {
public static void main(String[] args) {
// example of passing multiple values to a method using lambda
// notice that I do NOT have to specify the data type of a and b
Calculate add = (a, b) -> a + b;
Calculate difference = (a, b) -> Math.abs(a - b);
Calculate divide = (a, b) -> (b != 0 ? a / b : 0);
Calculate multiply = (a, b) -> a * b;
System.out.println(add.calc(3, 2));
System.out.println(difference.calc(5, 10));
System.out.println(divide.calc(5, 0));
System.out.println(multiply.calc(5, 3));
}
}
@FunctionalInterface
interface Calculate {
int calc(int x, int y);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment