
A lambda expression is an anonymous function. Simply put, it's a method without a declaration, i.e., access modifier, return value declaration, and name.Lambda expression facilitates functional programming.
Following are the important characteristics of a lambda expression -
Optional type declaration - No need to declare the type of a parameter. The compiler can inference the same from the value of the parameter.
Optional parenthesis around parameter - No need to declare a single parameter in parenthesis. For multiple parameters, parentheses are required.
Optional curly braces - No need to use curly braces in expression body if the body contains a single statement.
Optional return keyword - The compiler automatically returns the value if the body has a single expression to return the value. Curly braces are required to indicate that expression returns a value.
Lambda syntax :
Parameters => Executed code
In JAVA 1.8, it has been introduced. Here is an example
//Method: getFilterCandidateInfo
//Purpose: Get candidates filtered by RollNumber
public List<Candidate> getFilterCandidateInfo(String rollNumber){
return getAllCandidateInfo()
.stream() //converts the List to Stream
.filter(rn -> rn.equals(rollNumber)) //Performs the Filter Operation
.collect(Collectors.toList());//converts stream to List
}
You can figure out that inside
.filter(rn -> rn.equals(rollNumber)) we are using
-> operator. It is the LAMBDA way.
The same in C# will be
public List<Candidate> getFilterCandidateInfo(String rollNumber){
return getAllCandidateInfo().Where(rn=>rn.equals(rollNumber)).ToList();
}
More about Lambda:
https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html
https://msdn.microsoft.com/en-IN/library/bb397687.aspx
Hope this helps.
--
Thanks & Regards,
RNA Team
Crniranjanraj, if this helps please login to Mark As Answer. | Alert Moderator