Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand lambda

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to understand lambda". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to understand lambda".

Lambda syntax

If the type is an interface and there is only one method that needs to be implemented, you can use an expression as the implementation of the interface.

Understand lambda

Java lambda has the ability to express a complete method signature (with return value), but always writing full lambda in development can not achieve the effect of simplifying the code, so we prefer to use the simplified method of lambda in development.

Package com.karmic.lambda;import java.util.function.Function;import java.util.function.Supplier;/** * @ author karmic * / public class LambdaDemo {public static void main (String [] args) {/ / lambda all Function f0 = (Function) (String s)-> {return Integer.parseInt (s);} / only omit {} and return Function F1 = (Function) (String s)-> Integer.parseInt (s) on one line; / / simplified return type Function f2 = (String s)-> Integer.parseInt (s); / / simplified input parameter type Function f3 = (s)-> Integer.parseInt (s) / / single input parameter omitted parameter () Function f4 = s-> Integer.parseInt (s); / / use: simplified expression Function f5 = Integer::parseInt when the method signature and return value of the expression call are consistent with the interface / / Class:: static method or object:: object method can be converted into a method with the same signature Supplier A1 = DemoBean::getA; Supplier a2 = new DemoBean ():: getB / / Class:: the object method can be converted into a signature. The first input parameter is the class object, and the rest of the signed interfaces Function bf = DemoBean::getB;} static class DemoBean {public static String getA () {return "A" } public String getB () {return "B";}

![在这里插入图片描述](https://img-blog.csdnimg.cn/2020112110170750.png#pic_center

Common applications in lambda Development

Gracefully bypass NullPointException

Package com.karmic.lambda;import java.util.Optional;/** * @ author karmic * / public class NullExceptionAvoid {public static void main (String [] args) {AA = new A (new B (null)); String s = Optional.of (a) .map (A::getB) .map (B::getC) .map (C::getS) .orElse (null); System.out.println (s) } static class A {B b; public A (BB) {this.b = b;} public B getB () {return b;}} static class B {C c Public B (C c) {this.c = c;} public C getC () {return c;}} static class C {String s Public C (String s) {this.s = s;} public String getS () {return s;}

Relinquish control of code execution

/ / Create bean instance. If (mbd.isSingleton ()) {sharedInstance = getSingleton (beanName, () -) {try {/ / Spring source code, the control over whether the createBean method is executed, and the getSingleton control to determine whether the bean has been created. Then decide whether to call the createBean method return createBean (beanName, mbd, args) } catch (BeansException ex) {/ / Explicitly remove instance from singleton cache: It might have been put there / / eagerly by the creation process To allow for circular reference resolution. / / Also remove any beans that received a temporary reference to the bean. DestroySingleton (beanName); throw ex;}}); bean = getObjectForBeanInstance (sharedInstance, name, beanName, mbd) }

MR data processing

Package com.karmic.lambda;import java.util.ArrayList;import java.util.Arrays;import java.util.List;import java.util.Map;import java.util.function.Function;import java.util.stream.Collectors / * * @ author karmic * / public class StreamWordCountDemo {public static void main (String [] args) {@ SuppressWarnings ("serial") List list = new ArrayList () {{add ("hello world"); add ("hello java") Add ("hello python"); add ("hello kotlin");}} Map map = list.stream () .map (s-> s.split (")) .flatMap (Arrays::stream) .flatMap (Collectors.groupingBy (Function.identity (), Collectors.counting (); System.out.println (map) }} Thank you for your reading, the above is the content of "how to understand lambda". After the study of this article, I believe you have a deeper understanding of how to understand lambda, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report