From the beginning of java 8 release, We are hearing about something called lamda . Here i try to explain all that one should know about this mysterious thing called lamda which is one of the feature introduced in Java 8. For a functional programmer ( who uses different programming languages than java) , lamda expressions is something which they can relate to . A traditional object oriented java programmer will not expect such a feature to get introduced in Java. I will try to explain how lamda differs from object orientedness of java.
Lets look at a case where we have to execute a piece of code multiple times . An example may be an ActionListener on a button click
// Defined using Inner classes
button.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
System.out.println("Thanks for clicking!");
}
});
Here , The handle method is executed multiple times on each button click . This is an example of a code which is executed multiple times in an application . Same can be applied to a
Comparator which is passed to a Collections.sort method for sorting. The compare method inside the defined comparator is called multiple times.Uptill now these kind of codes were passed as an object where a method is defined inside it . From now onwards we could pass it as a block of code, Which is to be executed . This is more or less what lamda expressions are
Lets look at it in detail. Let me take the example of a String Comparator which does the comparison by length . I will write the old way of doing it
ArrayList<Strings> sortingNeeded = new ArrayList<String>();
sortingNeeded.add("looooooongOne");
sortingNeeded.add("shortOne");
Collections.sort(sortingNeeded, LengthComparator);
// LengthComparator code is defined as :
class LengthComparator implements Comparator<String> {
public int compare(String first, String second) {
return Integer.compare(first.length(), second.length());
}
}
Lets now do it the Lamda way :
ArrayList<Strings> sortingNeeded = new ArrayList<String>();
sortingNeeded.add("looooooongOne");
sortingNeeded.add("shortOne");
// The block of code which needed to be executed here is : Integer.compare(first.length(), second.length()) ,
//But since java is strongly typed, We have to provide the type of arguments as
// well, So the code comes to be :
Collections.sort(sortingNeeded, (String first, String second)-> Integer.compare(first.length(), second.length()) );
Whaat ? isnt ? Yes ,You have just seen one form of lambda expressions in Java: "parameters, the -> arrow, and an expression". . If the code carries out a computation that doesn’t fit in a single expression, write it exactly like you would have written a method: enclosed in {} and with explicit return statements. For example,
(String first, String second) -> {
if (first.length() < second.length()) return -1;
else if (first.length() > second.length()) return 1;
else return 0;
}
Now you may be asking what if we have return values , how do we specify the type of the return value . The answer is that you can never provide the return type , its always inferred from the context.
Also if the type of the arguments can be inferred , You could avoid that too ,
Comparator<String> comp = ( first, second)-> Integer.compare(first.length(), second.length());
here the type of first and second can be inferred from the context that it will be strings as the lamda function is assigned to a variable which is of the type string comparator. Hence we can avoid the type.
What if there is no argument type?
yes , ()-> System.out.println("Hello") is a valid lamda expression .
You are not at all expected to be clear about lamda expressions with this post . There will be a lot of questions , Where is this code executed ? What if an exception occurs ? Where exactly do you use the lamda expressions . All this and much more on coming topics. Stay tuned. Happy Coding :)