In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article is about how to use Lambda expressions in java. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
First of all, the simplest application of Lambda expression is as follows
Lambda expression String lam= "first acquaintance Lambda"; new Thread (()-> System.out.println (lam)) .start (); traditional method String tradition= "traditional method"; new Thread (new Runnable () {@ Override public void run () {System.out.println (tradition);}}) .start ()
Output result
It's simple, isn't it? Omitting a lot of code, you can find here that a Lambda expression has the same effect as creating an anonymous class in Thread. We can think of the Lambda expression itself as representing an anonymous class.
This is the greatest use of Lambda, and of course Lambda expressions can only create interface interface objects. It is not possible to create classes, nor can abstract classes, as long as they are classes.
First, I define a custom interface that can be used to test
@ FunctionalInterfacepublic interface Lam {/ / the method called by the Lambda expression void bda (); / / the default method of the interface default void test () {System.out.println ("I am the default method");}; / / the static method of the interface static void test1 () {System.out.println ("I am the static method");}} the first thing you need to know about using Lambda expressions
The 1.Lambda expression can only be the creation of the interface interface (PS can be seen from the above example, Runnable is the interface, you can view the source code), and this interface can only contain one method (except the default method and the static method). To create both default and static methods in an interface, you must implement the method body as shown in the following figure
two。 If you use Lambda expressions to create class class, you will get the following error "Target type of a lambda conversion must be an interface". If you are afraid that your defined interface does not conform to the specification of Lambda expressions, you can add @ FunctionalInterface to the interface interfaca.
The canonical representation format of 3.Lambda expressions (parameters)-> {statements;}. At some point, you can also simplify this format.
/ / when the method defined by the interface has no parameters and there is only one sentence of code that you want to perform the operation, Lambda will automatically return a sentence of code without adding {} Lam lam1= ()-> System.out.println ("No parameters").
You can find that the curly braces {} are gone, because if there is only one sentence in the following code, {} can be omitted.
We modify the calling method parameters defined by the Lam interface to add a formal parameter s of type String
/ / method called by Lambda expression void bda (String s)
At this point, if we use Lambda expressions, we can do this.
/ / when the method defined by the interface has parameters and the operation you want to perform has only one sentence of code, Lam lam1=e- > System.out.println (e); / / there is a simplified version of Lam lam1=System.out::println; lam1.bda ("4556")
You will also find that the parentheses in the preceding () are gone, because () can be omitted when there is only one parameter.
Of course, there are times when you have to execute a lot of code, then you can do this.
/ / when the method defined by the interface has parameters and the operation you want to perform has many sentences of code, Lam lam1 = (String e)-> {String a = e + "add"; System.out.println (a);}; lam1.bda ("test+")
The output is as follows
Of course, you will also ask if Lambda expressions can return something. This is for sure. First of all, let's modify the Lam interface method above.
/ / method called by Lambda expression String bda (String s)
Let the bda method return a string value, this time if we use Lambda
/ / Lam lam1=s-> {System.out.println (s); return "I am the returned data";}; lam1.bda ("test1"); System.out.println (lam1.bda ("test2")) when the method defined by the interface has a return value
The result of the operation:
The summary Lambda expression is used to create an anonymous interface object, that is, it is itself an anonymous instance of the interface. It's just that there are some restrictions on this interface.
Techniques of Lambda expressions Lambda expressions can only be used to simplify the creation of interfaces that contain only one public method
Rules
1. Can only be an interface
Otherwise, report: Target type of a lambda conversion must be an interface
two。 There can be only one public method
Otherwise, report: Multiple non-overriding abstract methods found AInterface
Or AInterface is not a functional interface
Parenthesis form
TestA ((int I, int j)-> {}); parameters should be the same as the API
Public class Go {public static void main (String a []) {/ / correct demonstration testA ((int I, int j)-> {}); / / error demonstration: Multiple non-overriding abstract methods found xxx; can only have one public method testB ((int I, int j)-> {}); / / error demonstration: Target type of a lambda conversion must be an interface Can only be interface testC ((int I, int j)-> {});} public static void testA (AInterface t) {} public static void testC (CInterface t) {} public static void testB (BInterface t) {} interface AInterface {void xxx (int I, int j);} interface BInterface {void xxx (int I, int j); void YYY (int I, int j) } abstract class CInterface {abstract void xxx (int I, int j);}} double colon expression
The double colon must be followed by a static method
Otherwise, an error is reported: Non-static method cannot be referenced from a static context
The method after the double colon is the same as the interface method parameter
The permissions of methods and interfaces can be different.
Return type: if the method in the interface is void, the method after the double colon can return any type, otherwise it should be consistent.
Public class Go {public static void main (String a []) {/ / previously written testA (new AInterface () {@ Override public void xxx (int I, int j) {}}); / / correct, relative to the xxx side in the interface, this is changed to static and changed to testA (Go::mydog) / / correct, add the return type and public and replace it with private, which is also ok testA (Go::mydog2); / / error: Non-static method cannot be referenced from a static context testA (Go::mydog3); / / it is also ok. AInterface aInterface = Go::mydog; testA (aInterface);} public static void testA (AInterface t) {t.xxx (1,2);} interface AInterface {void xxx (int I, int j);} public static boolean mydog (int I, int j) {System.out.println ("mydog" + I + "&" + j); return false } private static void mydog2 (int I, int j) {System.out.println ("mydo2" + I + "&" + j);} public void mydog3 (int I, int j) {System.out.println ("mydog3" + I + "&" + j);}} Thank you for reading! This is the end of this article on "how to use Lambda expressions in java". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
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.