Thursday, 22 June 2017

Java 8 new features

The main feature of Java 8, Lambda expressions, also called behaviour parameterization, it allows transferring code into methods. Another new feature, Method-reference gives a succinct and descriptive code.  


Lambda expression 

Mathematically, an expression with parameter variables has been called a Lambda expression.  
for instance: f(x,y)=x+2y

In programming, Lambda function stands for a function defined without a name (identifier). Before Java 8, in order to pass a function, we need to declare a class that implements an interface. By this way, a developer may pass a function wrapped in an object. It is expensive in some sense. 

So Java 8 introduces a lambda expression for cheaply passing a block of code around. Java compiler is becoming smarter and smarter. It may infer parameter types from a functional interface (one method interface). Don't be scared of this new term.  Actually, we have used it for a quite long time, for instance, Comparator or Comparable interfaces.  

As a known functional interface, by using the lambda expression, passing a function around can be greatly simplified. 

The following example, the comparing function can be directly passed as an argument parameter. I think it exactly follows the math definition, f(x,y)=x+2y

package Lambda;

import java.util.ArrayList;
import java.util.List;
import java.util.Collections;

/**
 *
 * @author YNZ
 */
public class UsingLambadaExpression {

    public static void main(String[] args) {
        List users = new ArrayList<>();
        users.add(new User("Mike"));
        users.add(new User("Jeppe"));
        users.add(new User("Yichun"));
        Collections.sort(users, (o1, o2) -> o1.getName().compareTo(o2.getName()));
        System.out.println("" + users);
    }

}

class User {

    private String name;

    public User(String name) {
        this.name = name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    @Override
    public String toString() {
        return this.name;
    }
}
Note: You can add annotation or final modifier to the lambda parameters. Note: It is not legal for lambda to return a value in a branch. Note: Functional interface contains a single abstract method.

Lambda Expression 3 ingredients:
(1) a block of code; (2) parameters; (3) values for the free variables, which are not defined in the block and transferred by the parameters.

a block of code and values of free values is a closure. Actually, it is implemented as a transferring by an object has a single method, so the free values have been initialized within that instance.

Method Reference 

 is used to simply certain Lambda expression, for instance:
(x->System.out.println(x))
We know there is already existed a method that may exactly operate the parameter as expected. In this case Lambda expression can be greatly simplified by using method reference.
 System.out::println 
Constructor reference, it is just like method reference, but using something like Class::new. It invokes its constructor. It equivalent to the lambda expression,i.e.
 x-> new constructor(x) 
Constructor reference can be used with the array type, i.e. int[]::new, i.e.
 x-> new int[x] 

No comments:

Can Jackson Deserialize Java Time ZonedDateTime

Yes, but must include JSR310. Thus ZonedDateTime can be deserialized directly from JSON response to POJO field. <dependency> <g...