October 22, 2018

Working with lambda expression in java 8

Lambda Expression in java 8



Source code will be available in github also


Function Interface 1



package com.cfed.lambda;
interface NumericTest {
int computeTest(int n);
}


/**
 *  Function Interface 2
 */
package com.cfed.lambda;

/**
 * @author consumerfed
 *
 */
public interface StringManip {

public String operate(String s);

}


/**
 * sample lambda experssion in java 1.8
 *
 */
package com.cfed.lambda;

public class MainClass {

public static void main(String args[]) {

NumericTest square  = (value) -> (value * value);
System.out.println(" the object square returns :"+square.computeTest(5));

NumericTest cube  = (value) -> (value * value * value);
System.out.println(" the object cube returns :"+cube.computeTest(5));

StringManip concatStr = (val) -> (val+val);
System.out.println(" String concat is "+concatStr.operate("itsection"));

StringManip reverseStr = (str) ->{
String result = "";

for(int i = str.length()-1; i >= 0; i--)
result += str.charAt(i);

return result;

};

System.out.println(" The reverse of the string is :"+reverseStr.operate("consumerfed"));
}

}

Output

 the object square returns :25
 the object cube returns :125
 String concat is itsectionitsection
 The reverse of the string is :defremusnoc

No comments:

Post a Comment

Your feedback may help others !!!

Facebook comments