First chapter lambda expressions
The Lamada expression, the most important new feature in Java SE 8, has long been considered a missing feature in Java, and its appearance has made the entire Java language complete.
At least for now, in this section you will learn what lambda is and why he is an excellent part. New technologies such as single abstract methods and functional interfaces are also introduced.
Why is a lambda expression.
Just like closures, lambda expression makes some structures leaner and better readable, especially when there are processing inner classes. The following example is a code block implemented with an inner class:
Runnable Runnable = new Runnable () {
@Override public void Run () {
System.out.println ("Running ...");
}
}
In fact, the code can be replaced with a lambda expression with the following short code:
Runnable Runnable = ()-System.out.println ("Running ...")
In other words, if you need to pass a runnable interface to the Java.util.concurrent.Executor class, you need to
Executor.execute (New Runnable () {
@Override
public void Run () {
System.out.println ("Running ...");
}
});
But you can use lambda expressions to achieve the same effect, using the following code;
Executor.execute ((), System.out.println ("Running ...");
Short, elegant, clean and expressive.
Modify the interface.
Before you dive into lambda, let's look at the Defalut and static methods that can be included in the interface in Java 8. This section explains what the static and Defalut methods are,
Why is there a need for drastic modifications to bring Lambda into the Java core library?
Default method
Adding functionality to an interface is a safe way to extend the interface. However, you will eventually get two interfaces that are functionally similar. If you only need to inherit one of the two interfaces is acceptable,
But if you need to add new features to hundreds of interfaces, this becomes a serious problem.
This is the problem that Java designers are facing, and they are trying to add lambda to java8 to support dozens of interfaces in a collection framework. These interfaces need to add new methods to support lambda, but extending all of the interfaces will only yield one-fold interfaces, some of which may be crowned with List2 or extendeset ugly names.
Instead, they solved the problem by adding the default method. The default method is a good method that has been implemented in the interface. An interface implementation class does not have to implement the default method,
This means that you add new methods in the interface without breaking backward compatibility issues at the same time.
Define the default method in the interface, just add the "default" keyword before the method signature. In addition, add the parentheses and code in it.
You can learn in the future that many interfaces in Java8 have default methods.
When you implement an interface with a default method, you have these choices:
Ignoring the default method, directly inheriting them takes effect.
Re-declare the default method, and change it to an abstract method.
Overloads the default method.
Remember that Java supports the default method primarily for backwards compatibility. You should never write programs without classes.
static methods.
A static method is shared by all instances of the class, and after Java8, you can use static methods in the interface so that all static methods associated with the interface can be used by interfaces, rather than a helper class.
The signature of a static method is similar to the signature of the default method, instead of default keyword, with the static keyword, static is public in the interface.
Static methods are rarely seen in interfaces, with nearly 30 interfaces in the Java.util package, and only two have static methods.
Upgrading to Java 8