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

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

Share

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

This article introduces the knowledge of "how to use Lambda expressions in Java". Many people will encounter this dilemma in the operation of actual cases, 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!

Catalogue

Understand the development of functional interfaces and Lambda expressions

Lambda expressions and grammars

Let's take a look at the specific use.

What do you need to pay attention to?

Practical Application of Lambda

1. Sort the collection

two。 Ergodic set

3. Traversing the collection (with conditions)

4. Instead of Runnable, start a thread

Understand the development of functional interfaces and Lambda expressions

Any interface contains only one abstract method, which is a functional interface.

/ * Development of lambdab expressions * / public class TestLambda1 {/ / 3. Static inner class static class Like2 implements ILike {@ Override public void lambda () {System.out.println ("I like lambda2");}} public static void main (String [] args) {ILike like=new Like (); like.lambda (); like=new Like2 (); like.lambda (); / / 4. Local inner class class Like3 implements ILike {@ Override public void lambda () {System.out.println ("I like lambda3");}} like=new Like3 (); like.lambda (); / / 5. Anonymous inner class like=new ILike () {@ Override public void lambda () {System.out.println ("I like lambda4");}}; like.lambda (); / / 6. Simplify like= ()-> {System.out.println ("I like lambda5");}; like.lambda ();} / / 1 with lambda. Define a functional interface interface ILike {void lambda ();} / / 2. Implement class class Like implements ILike {@ Override public void lambda () {System.out.println ("i like lambda");}} Lambda expression and syntax

Lambda allows functions as arguments to a method (functions are passed into methods as arguments)

/ / 1. No parameters are required, and the return value is 5 ()-> 5 / / 2. Receives a parameter (numeric type) and returns twice its value x-> 2 * x / / 3. Take 2 parameters (numbers) and return their difference (x, y)-> x-y / / 4. Receive 2 int integers and return their sum (int x, int y)-> x + y / / 5. Accept a string object and print it on the console without returning any value (it looks like returning void) (String s)-> System.out.print (s)

In other words, there is no need to declare the type of the parameter in advance, the parentheses can be absent, the curly braces can be absent, and the return value can be absent.

Let's see how to use public class java8_Lambda_Test {public static final void main (String [] args) {Operator addOper = (a, b)-> a + b; Operator reduceOper = (a, b)-> a-b; Operator multiplyOper = (a, b)-> a * b; Operator DivideOper = (a, b)-> a / b; java8_Lambda_Test test = new java8_Lambda_Test () System.out.println (test.getResult (3,3, addOper)); / 6 System.out.println (test.getResult (3,3, reduceOper)); / / 0 System.out.println (test.getResult (3,3, multiplyOper)); / / 9 System.out.println (test.getResult (3,3, DivideOper)); / / 1} public int getResult (int a, int b, Operator operator) {return operator.convert (a, b) } public interface Operator {int convert (int a, int b); default int get (int a) {return a;} what do you need to pay attention to

1.lambda expressions can only refer to outer local variables marked with final

two。 You can access the outer local variables directly in the lambda expression

The practical application of Lambda 1. Sort the collection

Problem description: in a list that stores Student objects, sort by age, and output the sorted list

Student class

Public class Student implements Comparable {private String name; private int age; public String getName () {return name;} public void setName (String name) {this.name = name;} public int getAge () {return age;} public void setAge (int age) {this.age = age;} @ Override public int compareTo (Student student2) {int result = this.age-student2.age Return result;}} List list=new ArrayList (); Student s1=new Student (); s1.setName (Zhang San); s1.setAge (28); list.add (S1); Student s=new Student (); s.setName (Li Si); s.setAge (22); list.add (s); sortUsingJava8 (list) List.forEach ((student)-> System.out.println (student.getName () + ":" + student.getAge (); / / sort static void sortUsingJava8 (List list) {Collections.sort (list, (student1, student2)-> student1.compareTo (student2)) using java 8;}

Lai Kangkang uses Java7 to sort things like this:

/ use java 7 to sort private void sortUsingJava7 (List list) {Collections.sort (list, new Comparator () {@ Override public int compare (String S1, String S2) {return s1.compareTo (S2);}});} 2. Traversing the set / / first prepare a set List list=new ArrayList (); Student s1=new Student (); s1.setName ("Zhang San"); s1.setAge (28); list.add (S1); Student s=new Student (); s.setName ("Li Si"); s.setAge (22); list.add (s) / / use lambda expressions and function operations (functional operation) list.forEach ((student)-> System.out.println (student.getName () + ":" + student.getAge (); 3. Traversing the set (conditional) / / conditional printing the names of students aged 20 or older list.forEach ((student)-> {if (student.getAge () > = 20) System.out.println (student.getName ());}); 4. Instead of Runnable, start a thread Runnable r = new Runnable () {@ Override public void run () {/ / to do something}}; Thread t = new Thread (r); t.start ()

Now write like this:

Runnable r = ()-> {/ / to do something}; Thread t = new Thread (r); t.start ()

You can also write:

Thread t = new Thread (()-> {/ / to do something / / such as for for (int I = 0; I < 100; iSuppli +) {System.out.println (I);}}); t.start; that's all for "how to use Lambda expressions in Java". 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.

Share To

Development

Wechat

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

12
Report