In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Java Lambda expression method reference and constructor reference example analysis, in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
A little bit of eye contact
If the code block of a Lambda expression has only one code, you can also use method references and constructor references in the code block to make the Lambda expression more concise.
Kinds
Example
Description
Corresponding Lambda expression
Reference class method
Class name:: class method
All the parameters of the implemented method in the functional interface are passed to this kind of method as parameters.
The name of the class (a dint bpcr...) Class method (aformab,...)
An instance method that references a specific object
Specific object:: instance method
All the parameters of the implemented method in the functional interface are passed to the method as parameters.
(a) b,...)-> specific object. Example method (aformab,...)
An instance method that refers to a class of objects
Class name:: instance method
The first parameter of the implemented method in the functional interface is the caller, and all the subsequent parameters are passed to the method as parameters.
(a) b,...)-> a. Instance method (b,...)
Reference constructor
Class name:: new
All parameters of the implemented method in the functional interface are passed to the constructor as parameters.
(a _ dint b,...)-> new class name (a _ r b,...)
Second actual combat
1 code
Import javax.swing.*;@FunctionalInterfaceinterface Converter {Integer convert (String from);} @ FunctionalInterfaceinterface MyTest {String test (String a, int b, int c);} @ FunctionalInterfaceinterface YourTest {JFrame win (String title);} public class MethodRefer {public static void main (String [] args) {/ / the following code uses Lambda expressions to create Converter objects / / Converter converter1 = from-> Integer.valueOf (from); / / method references instead of Lambda expressions: reference class methods. / / all the parameters of the implemented method in the functional interface are passed to this kind of method as parameters. Converter converter1 = Integer::valueOf; Integer val = converter1.convert ("99"); System.out.println (val); / / output integer 99 / / the following code uses the Lambda expression to create a Converter object / / Converter converter2 = from-> "fkit.org" .indexOf (from); / / method reference instead of Lambda expression: an instance method that references a specific object. / / all the parameters of the implemented method in the functional interface are passed to the method as parameters. Converter converter2 = "fkit.org":: indexOf; Integer value = converter2.convert ("it"); System.out.println (value); / / output 2 / / the following code uses the Lambda expression to create a MyTest object / / MyTest mt = (a, b, c)-> a.substring (b, c); / / method reference instead of Lambda expression: an instance method that references an object of some kind. / / the first parameter of the implemented method in the functional interface is used as the caller, and / / all the subsequent parameters are passed to the method as parameters. MyTest mt = String::substring; String str = mt.test ("Java I Love you", 2,9); System.out.println (str); / / output: the following code uses the Lambda expression to create the YourTest object / / YourTest yt = (String a)-> new JFrame (a); / / the constructor reference replaces the Lambda expression. / / all the parameters of the implemented method in the functional interface are passed to the constructor as parameters. YourTest yt = JFrame::new; JFrame jf = yt.win (my window); System.out.println (jf);}}
2 run
992va I Lojavax.swing.JFrame [frame0,0,0,0x0,invalid,hidden,layout=java.awt.BorderLayout,title= my window, resizable,normal,defaultCloseOperation=HIDE_ON_CLOSE,rootPane=javax.swing.JRootPane [, 0JJJRootPaneBook Layout.javax.swing.JRootPaneEncoding RootLayoutDivision], rootPaneCheckingEnabled=true]
This is the answer to the sample analysis questions about the method reference of Java Lambda expression and the constructor reference. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.
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.