I. Overview
A collection is a tool that our Java programmers need every day, and without a collection, Java programmers can hardly do anything, and our daily work is doing different things to the collection.
Although the functionality of the collection is strong enough, when we are faced with complex business problems, it becomes disgusting to take advantage of the primitive collection operations.
So the lambda and stream APIs appeared in java8 to use the collection in a more elegant way.
Yes, it is the collection, when we are now using NoSQL, various log analysis, and so on big data operations, we can not use the original database of SQL operations to help us complete such as sorting, summing, grouping and other operations,
We're desperate for a tool to do our job, and it's nice to know that the stream did it and it was elegant.
So what is the relationship between functional programmatic programming and STREAM,LAMBDA? Because Java introduced the programming paradigm of functional programming to solve this problem,
Therefore, our starting point should be functional programming.
Two. From functional programming to Lambda
Behavior parameterization, a very abstract concept, in fact, we are in use every day, very much like a strategy pattern, such as we in JS constantly passed in the same callback function.
In Java8, the foundation of functional programming is lambda, which is the basis of the entire Java functional programming.
This is not to say the exact meaning of functional programming, because no one can speak clearly.
What we want to do is introduce lambda.
Take a look at the following example:
The way we used to thread runnable:
New Runnable () { @Override publicvoid run () { for(;;) SYSTEM.OUT.PRINTLN ("Thread is running ...");} ; New Thread (runnable). Start ();
In the past, we used anonymous inner classes to write a Runnbale implementation, in fact, in the above code, only the red part is useful.
When we write a lot of this code, we probably know that we really want to create a behavior (or an implementation), there is no need to write a lot of template code every time.
So, in Java8, we can write the following code.
New Thread ((),{ for(;;) System. out. println ("thread is running ... " ); }). Start ();
Hey, now we've written a lambda expression to describe our behavior.
Three. The nature of lambda expressions
The nature of the lambda expression is the same as the anonymous inner class, and its conditions are more stringent.
The following example shows that the lambda and anonymous inner classes are actually a thing:
Runnable run = (){ for(;;) System. out. println ("thread is running ... " ); };
See the above code does not compile the problem? This proves that Lambda is essentially an implementation of an anonymous inner class.
From here we can see that, even if there is a lambda in Java, it does not fundamentally overturn our previous worldview.
On top of that, the condition of lambda is more harsh, why?
We know that an anonymous inner class can implement multiple methods, but lambda can only implement one.
Therefore, in order to better express the use of lambda in java8, a new concept has emerged, that is, the functional interface.
See, Lambda and functional interfaces appear, and the function interface itself is for lambda service.
So, what is a functional interface? It is simply that an interface (abstract class) has only one method to implement, and we are called a functional interface.
To express this concept, an annotation is used to express the concept in Java8,
public @interface Functionalinterface {}
This annotation is just a markup annotation, and the only thing that can be provided is to indicate a syntax error before compiling, like @override.
Even if an interface does not have this annotation, it does not prevent the interface from becoming a functional interface.
001 Why Java needs functional programming