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

What is the Lamda expression in java

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

What is the Lamda expression in java? I believe many inexperienced people don't know what to do about it. Therefore, this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

Lamda expression

1. The eleventh alphabet in the Greek alphabet with the English name Lamda.

2. Avoid too many anonymous inner class definitions

3. You can make your code look simple

4. Get rid of a bunch of meaningless code and leave the core logic

5. Its essence belongs to the concept of functional programming.

(params)-> expression [expression]

(params)-> statement [statement]

(params)-> {statements}

A-> System.out.println ("I like lamda-- >" + a)

New Thread (()-> System.out.println ("multithreaded learning.") .start ()

How to understand

1. Understanding Functional interface (functional interface) is the key to learning java8 lamda expressions.

2. Definition of functional interface:

Any interface that contains only one abstract method is a functional interface.

Public interface Runnable {public abstract void run ();}

For functional interfaces, we can create objects for that interface through lamda expressions

The code deduces the lambda expression package com.haiyang.lamda;public class TestLamda01 {/ / 3, static inner class, and also implements ILike interface static class Like2 implements ILike {@ Override public void lamda () {System.out.println ("I like lamda2");}} public static void main (String [] args) {ILike like = new Like (); like.lamda () Like = new Like2 (); like.lamda (); / / 4, local inner class class Like3 implements ILike {@ Override public void lamda () {System.out.println ("I like lamda3");}} like = new Like3 (); like.lamda () / / 5. Anonymous inner class, no class name. Must rely on interface or parent class like = new ILike () {@ Override public void lamda () {System.out.println ("I like lamda4");}}; like.lamda (); / / 6, lamda expression like = ()-> {System.out.println ("I like lamda5") }; like.lamda ();} / 1, define an interface interface ILike {abstract void lamda ();} / 2, implementation class class Like implements ILike {@ Override public void lamda () {System.out.println ("I like lamda1");}}

Output:

Lamda simplified package com.haiyang.lamda;public class TestLamda02 with one parameter {public static void main (String [] args) {ILive live = null; / / Lamda simplified live = (int a)-> {System.out.println ("I live you" + a);}; live.live (1) / / Lamda simplified parameter type live = (a)-> {System.out.println ("I live you" + a);}; live.live (2); / / Lamda simplified parentheses live = a-> {System.out.println ("I live you" + a);}; live.live (3) / / Lamda simplified curly braces (with only one statement) live = a-> System.out.println ("I live you" + a); live.live (4);}} interface ILive {abstract void live (int a);}

Output:

Lamda simplified package com.haiyang.lamda;public class TestLamda02 with multiple parameters {public static void main (String [] args) {ILive live = null; / / Lamda simplified live = (int a minint b)-> {System.out.println ("I live you" + aquib);}; live.live (1mem1) / / Lamda simplified parameter type (all should be removed if you want to remove it) live = (I live you)-> {System.out.println ("I live you" + aforb);}; live.live (2Power2); / / Lamda simplified curly braces (when there is only one sentence) live = (aforb)-> System.out.println ("I live you" + awrb); live.live (3P3) }} interface ILive {abstract void live (int aline int b);}

Output:

After reading the above, have you mastered the method of what Lamda expression is in java? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

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