Introduction :
Lambda Expressions enable us to treat functionality as a method argument or code as data . Well, What is functionality as a method argument? Did we already use this before? Yes, the answer is yes, we already used this concept before with anonymous classes. In anonymous classes case, we are usually trying to pass functionality as an
argument to another method. Lambda expressions are also to do this. Although anonymous classe is often more concise than a named class, for classes
with only one method, even an anonymous class seems a bit
excessive and cumbersome.
Usage :
First let us discuss the usage of the Lambda Expressions then will go through the syntax of Lambda Expressions.Suppose that you are creating an application for car sellers. You
are asked to create a feature that search cars by certain criteria.
Car class can be represented as follows.
Car class can be represented as follows.
public class Car{
public enum Color {
BLACK, WHITE, RED
}
public String modelName;
public Color color;
public int engineCC;
public void printModelName() {
// ...
}
}
Now you need to implement a feature that search for cars that match one characteristic such as engine cc.One simplistic approach is create a method that searches for cars that match one characteristic, such as engine cc. The following method prints Model name that are greater than the specified engine CC.