In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "lambda expression instance analysis". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Lamda expression
λ the Greek alphabet has the eleventh alphabet and the English name is Lambda
Avoid too many anonymous inner class definitions
Its essence belongs to the concept of functional programming.
(params)-> expression [expression]
(params)-> statement [statement]
(params)-> {statements}
(a-> System.out.print ("I like lambda- >" + a))
New Thread (()-> System.out.println ("multithreaded learning.") .start ()
Why use lambda expressions
Avoid too many anonymous inner class definitions
It can make your code look simple.
Get rid of a bunch of meaningless code, leaving only the core logic.
You may say, I read the Lambda expression, not only do not feel concise, but feel more messy, do not understand. That's because we're not used to it. We use it a lot. Just get used to it.
The focus of this chapter
Understanding Functionl Interface (functional interface) is the key to learning java8 lambda expressions.
Definition of function interface
Any interface that contains only one abstract method is a functional interface
For functional interfaces, we can create objects for that interface through the lambda style.
Instance code
Static inner class, local inner class, anonymous inner class and lambda expression
Public class TestLambda1 {/ / static inner class static class like2 implements ilike {@ Override public void lambda () {System.out.println ("ilike lambda2");}} public static void main (String [] args) {ilike i=new like (); i.lambda (); i=new like2 (); i.lambda () / / Local inner class class like3 implements ilike {@ Override public void lambda () {System.out.println ("ilike lambda3");}} i=new like3 (); i.lambda () / / Anonymous inner class. Without the name of the class, you must use the interface or husband class i=new ilike () {@ Override public void lambda () {System.out.println ("ilike lambda4");}; i.lambda () / / simplify with lambda I = ()-> {System.out.println ("ilike lambda5");}; i.lambda ();}} / define a functional interface interface ilike {void lambda ();} / / implement interface class like implements ilike {@ Override public void lambda () {System.out.println ("ilike lambda");}
Lambda expression:
Public class TestLambda2 {public static void main (String [] args) {Ilove love=null; / / 2.lamda means simplified / / Ilove love= (int a)-> {/ / System.out.println ("I love you-- >" + a); /}; / simplified 1. Remove the parameter type / / love = (a)-> {/ / System.out.println ("I love you-- >" + a); /}; / / simplify 2 simplified parentheses love = a-> {System.out.println ("I love you-- >" + a);} / / simplify 3 remove curly braces love = a-> System.out.println ("I love-- >" + a) / / Summary / / the lambda expression can only be simplified to one line if there is one line of code. If there are more than one line, carry the code block / / if the interface is functional (there is only one method bit function method in the interface) / / multiple parameters can also be removed. To remove them all, you must add parentheses love.love (2). }} interface Ilove {void love (int a);} "lambda expression instance Analysis" ends here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.