Kava Lambda Expressions

From MyWiki
Revision as of 20:58, 10 April 2020 by George2 (Talk | contribs)

Jump to: navigation, search

Functional Interface

interface MyName{
  String computeName(String str);
}

Example1 - Numeric Test

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));
}

Example2 - Greeting Lambda

interface MyGreeting {
	String processName(String str);
}
 
public static void main(String args[]) {
	MyGreeting morningGreeting = (str) -> "Good Morning " + str + "!";
	MyGreeting eveningGreeting = (str) -> "Good Evening " + str + "!";
 
  	// Output: Good Morning Luis! 
	System.out.println(morningGreeting.processName("Luis"));
 
	// Output: Good Evening Jessica!
	System.out.println(eveningGreeting.processName("Jessica"));	
}