Lambda Expressions in Java Part 2
In the previous blog we learned about lambda expressions, its syntax, functional interfaces, etc. If you want to know more about lambda expressions visit the blog Lambda Expressions in Java In this blog we will go through the remaining topics of Lambda Expression.
Constructor Reference in Java 8
Constructor Reference behaves the same way as method reference but the difference is that rather than referencing the method it maps the functional interface with the constructor of the class. It is a mechanism used to refer to a constructor without creating the class. It is constructed using a class name and a new keyword.
Syntax
Let’s go through the example it will give us a more clear idea of the Constructor Reference
e.g.
Output
In the above code we have implemented a class wherein we have defined Constructor followed by the display statement and have passed some text in the println() method. In main() we have constructed the object of the Constructor using a :: operator and a new keyword.
Variable Scope
Many times we wish to be able to access variables from an enclosing method or class in a lambda expression. For example:
e.g.
In the above example we have defined the interface wherein we have declared two methods: first is the displayValues() method and second is the toString() method. In the VariableScopeEx class, we have created an object of that class and with the help of that object, we have called displayResult which is defined in the same class. In the displayResult() we have a lambda expression defined which returns the multiplication of two numbers.
In the Lambda expression, the value of a variable can be captured in the enclosing scope. In order to make sure that the captured value is well defined, a crucial restriction is defined in Java i.e. In lambda expressions we can only use the variables whose value is fixed(doesn’t change). Also, the variables that lambda expressions use are called effectively final. Effectively final variables are those whose values don't change after initialization. But if we try to change its values then the compiler will throw an error.
e.g.
If the lambda expression does not have any parameter we can provide empty parentheses just like with the no parameter method.
Output
In the above example we have initialized the variable numValue with 9 and we are again trying to reinitialize it by assigning the result of no1 * no2 which will give an error.
Like nested blocks, the body of the lambda expression also has the same scope. The same principles apply to name conflicts and shadowing. It is invalid to declare local variables or parameters having the same name as the local variables in a lambda expression.
e.g.
Output
Comparator Interface
As we have already seen in Interface in Java blog, we can sort an array of objects, but they should be the object of classes that implements the Comparable interface. We can sort the array of strings as the String class implements Comparable<String> and compareTo() method of String class(String.compareTo()) compares strings in alphabetical order. Now suppose there is a situation where we want to sort the string by incrementing the length rather than sorting in alphabetical order. We cannot have the String class implement the compareTo() method in two ways. In order to deal with this situation, we can use Collections.sort() method which has the parameters as an array and comparator(an object of a class that implements the Comparable interface)
Syntax
e.g.
Output
We can sort the elements in any order as we wish just by changing the return value inside the compare() method. Now suppose we want to sort the collection by more than one field then i.e. firstly if we want to sort by Employee Name and secondly by Employee experience, we can do it as follows:
e.g.