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 use of Lambda expressions in Java

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

Share

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

Editor to share with you what is the use of Lambda expressions in Java, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Lambda expression: 1. Introduction

First of all, Lambda expression is a new feature of Java8, which provides support for functional programming in Java programming, contributes to the simplicity of the code, and can replace most of the anonymous functions, especially for set traversal and collection operation, which greatly simplifies the code.

The body of the Lambda expression:

Functional interface:

Note: Lambda expressions must be used with functional interfaces. The so-called functional interface means that the interface with only one abstract method in the interface is the functional interface, which we can customize. JDK also has a large number of built-in functional interfaces.

1. The @ FunctionalInterface annotation modifies the interface, so this interface is a functional interface. There can be only one method. Here is a functional interface:

@ FunctionalInterfacepublic interface MyInteface {void eat ();}

2. If you do not add the @ FunctionalInterface** annotation, you can only write an abstract method in the interface, which can also be considered a functional interface:

Public interface MyInteface {void eat ();}

This is also possible.

3. There is only one case of functional interfaces, not only abstract methods, that is, methods that can inherit the Object class:

@ FunctionalInterfacepublic interface MyInteface3 {void eat (); @ Override String toString (); @ Override int hashCode ();} 2. Use of Lambda expressions: 1. Use in common methods

Student class:

@ FunctionalInterfacepublic interface Student {void eat ();}

Test class:

Public class Test {public static void main (String [] args) {Student stu = new Student () {/ / normal method, rewrite and use @ Override public void eat () {System.out.println ("I am a student");}; stu.eat () / / lambda expression writing: / / Parameter 1: overrides the only eat abstract method without parameters in the Student interface and makes a concrete implementation. So rewriting does not require signature / / parameter 2v-> expression fixed / / parameter 3: {concrete implementation} specific implementation of the only eat method in the Student interface Student stu2 = ()-> {System.out.println ("students eat") }; stu2.eat ();}}

Output:

I'm a student

Students eat.

2. the use of the method with parameters.

Student class:

@ FunctionalInterfacepublic interface Student {void eat (String food);}

Test class:

Public class Test {public static void main (String [] args) {/ / lambda rewrite the only parameter method for the Student interface: Student stu2 = (foodName)-> {System.out.println ("students are eating" + foodName);}; stu2.eat ("meat");}} / / output: students are eating meat 3, Lambda expressions to achieve multithreading

The method of creating multithreading was described in the previous article in Multithreading (1). Here you use lambda to create a thread:

Public class Test {public static void main (String [] args) {Thread t = new Thread (()-> {System.out.println ("this thread is created by lambda";}); t.start ();}} 4, Lambda expression operation

We can use lambda to operate operations with a lot less code:

Functional interface:

@ FunctionalInterfacepublic interface Calculator {T operation (T v1J T v2);}

Test class:

Public class Test {/ / calculation method public static Integer operator (Integer v1 Magi Integer v2 Magi Calculator calculator) {return calculator.operation (v1, v2);} public static void main (String [] args) {/ / use the lambda expression: / / here it means to pass in two parameters and return the running value int add = Test.operator (5jing10, (xMagy)-> {return xquoy). }); / / shorthand: you can write a lot less code, more briefly introducing int num1 = Test.operator (5 Test.operator 10, (x num2 y)-> x quoy); int num2 = Test.operator (10 more); System.out.println (add); System.out.println (num1); System.out.println (num2);}}

Output:

15 、 15 、 5

5. Lambda expression method reference

Sometimes we do not have to rewrite the method of the interface to do the specific implementation. If we have an existing method to implement, we can also reference the existing method to do the specific implementation of the method in the interface by way of method reference. This advantage is code reuse, such as the following:

Functional interface:

Public interface ResultOneParam {int method (int a);}

Test class:

Public class Test {public int addTo (int a) {return astat10;} public static int addTo2 (int a) {return axiom 10;} public static void main (String [] args) {/ / lambda rewrites the method method ResultOneParam lambda1= (a)-> astat10; / / method reference: the addTo2 method in Test is used to replace the method overridden method ResultOneParam lambda2= Test::addTo2 Int result1= lambda2.method (9); System.out.println (result1); / / method reference: reference off-the-shelf methods instead of method rewriting, so that methods can reuse Test test=new Test (); ResultOneParam lambda3=test::addTo; int result2= lambda3.method (9); System.out.println (result1);}} 6. Use of Lambda expressions for collections

Of course, Lambda is also very convenient for the operation of collections, which can be a lot less code:

Public class Test {public static void main (String [] args) {List list = Arrays.asList; / / lambda expression traverses the collection, rewriting the method of the Consumer interface list.forEach ((element)-> {System.out.println (element);}); / / abbreviation: list.forEach (element- > System.out.println (element)) / / lambda expression method reference for traversing the output list collection: list.forEach (System.out::print); / / even number of output list: list.forEach (element- > {if (element%2==0) {System.out.println (element);}}) }} above is all the content of the article "what is the use of Lambda expressions in Java?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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