In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article is to share with you about how to achieve lambada expressions and functional programming in java8, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
1. Definition: what is a lambada expression?
To give you a first impression, before java8, when creating a thread, we might have written:
Runnable r = new Runnable () {@ Override public void run () {System.out.println ("Hello");}}
This code uses an anonymous class, Runnable is an interface, here new a class that implements the Runnable interface, and then rewrites the run method, the run method has no parameters, and the method body has only one line of printed statements. In fact, we only care about the middle printed statements in this code, and the rest are superfluous. After java8, after we use the lambada expression, we can abbreviate it as:
Runnable r = ()-> System.out.println ("Hello")
The Lambda expression is an anonymous function (which is not entirely true for Java, but for now), simply put, it is an undeclared method, that is, no access modifiers, return value declarations, and names.
You can make a shorthand of it and write it where you need to use a method. Using this shorthand alternative is especially effective when a method is used only once and the definition is short, so you don't have to bother writing declarations and methods in the class.
2. Syntax of lambaba expressions
The syntax format of the lambda expression is as follows:
(parameters)-> expression or (parameters)-> {statements;}
A Lambda expression can have zero or more parameters
The type of the parameter can be explicitly declared or inferred from the context. For example, (int a) has the same effect as (a).
All parameters should be contained in parentheses and separated by commas. For example: (a, b) or (int a, int b) or (String a, int b, float c)
Empty parentheses indicate that the parameter set is empty. For example: ()-> 42
When there is only one parameter and its type can be deduced, parentheses () can be omitted. For example: a-> return Ayoga
The body of a Lambda expression can contain zero or more statements
If the body of the Lambda expression has only one statement, curly braces {} can be omitted. The return type of the anonymous function is the same as the body expression
If the body of an Lambda expression contains more than one statement, the expression must be enclosed in curly braces {} (forming a code block). The return type of the anonymous function is the same as that of the code block, and it is empty if it is not returned.
Here are some examples of lambada expressions:
(int a, int b)-> {return a + b;} ()-> System.out.println ("Hello World"); (String)-> {System.out.println (s);} ()-> 42 ()-> {return 3.1415}; 3. Why would java need lambada expressions?
Java is a first-class object-oriented language. Except for some simple data types, everything in Java is an object, even if the array is also an object, and the instance created by each class is also an object. Functions or methods defined in Java cannot be completely independent, nor can they take a method as an argument or return a method to an instance.
In the object-oriented world of Java, "abstraction" is the abstraction of data, while "functional programming" is the abstraction of behavior. In the real world, data and behavior coexist, and so are programs. Therefore, the emergence of lambada expressions in java8 makes up for the lack of java in abstracting behavior.
Second: functional interface 1, Definition: what is a functional interface?
Functional interface (Functional Interface) is a term used by Java 8 for a special type of interface. This type of interface defines only the interface of a unique abstract method (except for the implied public method of the Object object), so it starts out as an interface of type SAM (Single Abstract Method).
I was a little confused when I saw this concept for the first time. Because all the methods in the interface are public abstract (even if these two keywords are omitted, every method in the interface is implicitly abstract, and the method in the interface will be implicitly specified as public abstract (only public abstract, other modifiers will report errors), then the above definition becomes: the interface declared by only one method is a functional interface. But in fact, the functional interfaces I see in the code contain both one method and multiple methods, which makes me confused. For example, the following two functional interfaces: Runnable and Consummer:
Runnable:@FunctionalInterfacepublic interface Runnable {/ * * When an object implementing interface Runnable is used * to create a thread, starting the thread causes the object's * run method to be called in that separately executing * thread. *
* The general contract of the method run is that it may * take any action whatsoever. * * @ see java.lang.Thread#run () * / public abstract void run ();} java.util.function.Consummer:@FunctionalInterfacepublic interface Consumer {/ * * Performs this operation on the given argument. * * @ param t the input argument * / void accept (T t); / * * Returns a composed {@ code Consumer} that performs, in sequence, this * operation followed by the {@ code after} operation. If performing either * operation throws an exception, it is relayed to the caller of the * composed operation. If performing this operation throws an exception, * the {@ code after} operation will not be performed. * * @ param after the operation to perform after this operation * @ return a composed {@ code Consumer} that performs in sequence this * operation followed by the {@ code after} operation * @ throws NullPointerException if {@ code after} is null * / default Consumer andThen (Consumer
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.