Home  >  Article  >  Java  >  How to use Lambda expressions in Java?

How to use Lambda expressions in Java?

王林
王林forward
2023-04-21 20:31:141031browse

Introduction to Lambda expressions

  • #The essence of Lambda expression is just a "syntax sugar", which is inferred by the compiler and helps you convert and wrap it into regular code , so you can use less code to achieve the same functionality.

  • Lambda expressions are an important new feature in Java SE 8.

Lambda and anonymous inner classes

Lamda expression refers to application in SAM (SingleAbstractMethod, an interface containing an abstract method) environment A simplified form of definition.

Advantages of Lambda over anonymous inner classes

Conciseness (see "Functional Interface" below for details)

Lamda over anonymous inner classes Disadvantages of the class

  • #The interface corresponding to Lamda can only have one method.

  • The interface corresponding to the anonymous inner class can have multiple methods

Requirements for the interface

1.Lambda stipulates that there can only be one method that needs to be implemented in the interface (ie, abstract method).

In an interface, the following ones can exist at the same time: one abstract method (there can only be one), multiple default methods, and multiple static methods.

// There is another new feature in jdk 8: default. The method modified by default will have a default implementation and is not a method that must be implemented, so it does not affect the use of Lambda expressions.

2.@FunctionalInterface: Used to modify functional interfaces.

@FunctionalInterface can be written or not. This annotation is mainly used for compile-level error checking: when the interface does not conform to the functional interface definition, the compiler will report an error.

Correct example, no error reported:

@FunctionalInterface
public interface HelloWorldService {
    void sayHello(String msg);
}

Incorrect example, error reported:

The interface contains two abstract methods, which violates the definition of functional interface, prompting that the interface Multiple non-overridden abstract methods were found.

How to use Lambda expressions in Java?

Note: Adding or not adding @FunctionalInterface has no effect on whether the interface is a functional interface. This annotation only reminds the compiler to check whether the interface only contains one abstract method.

Variables and scope

  • Lambda expressions can only reference outer local variables marked final. That is, local variables defined outside the scope cannot be modified inside the lambda, otherwise a compilation error will be reported

  • The local variables of the Lambda expression do not need to be declared final, but they must not be modified by subsequent code (i.e. Implicitly with final semantics)

  • Lambda expressions are not allowed to declare a parameter or local variable with the same name as a local variable.

Format

Important features of lambda expressions

  • Optional type declaration: No need to declare parameter types, the compiler Parameter values ​​can be identified uniformly.

  • Optional parameter parentheses: One parameter does not need to be defined with parentheses, but multiple parameters need to be defined with parentheses.

  • Optional braces: If the body contains a statement, braces are not required. Example: () -> Sysout.out.println("Test");

  • Optional return keyword: If the body has only one expression return value, the compiler will automatically Return value, curly braces need to be specified to indicate that the expression returns a value.

Simple example of Lambda expression

1. No parameters are required, the return value is 5

() -> 5

2. Receive one parameter (number type) and return 2 times its value

x -> 2 * x

3. Accept 2 parameters (number) and return them The difference

(x, y) -> x – y

4. Receive 2 int type integers and return their sum

(int x, int y) -> ; System.out.print(s)

Syntax format

Format:

(parameters) - > statement or (parameters) ->{ statements; }

Left side: the parameter list of the Lambda expressionRight side: the functions that need to be performed in the Lambda expression (Lambda body)

Syntax format 1: no parameters, no return value

##() -> Sysout.out.println("Test") ;

Syntax format 2: There is one parameter and no return value

(X)-> Sysout.out.println(x );

Grammar format three: If there is a parameter bracket, you don’t need to write it

##X-> Sysout.out.println(x) ;

Grammar format 4: There are more than two parameters, a return value, and there are multiple statements in the Lambda body. {}

needs to be used in the syntax body. <pre class="brush:java;"> Comparator&lt;Integer&gt; comparator = (o1, o2) -&gt; { System.out.println(&quot;Test&quot;); return Integer.compare(o1, o2); };</pre>

Grammar format 5: Only one statement is used in the Lambda body, return and {} can be omitted

##Comparator comparator = (o1, o2)-> Integer.compare(o1, o2);

语法格式六:表达式的参数列表的数据类型可以省略不写,JVM编译器通过上下文推断出数据类型

(x ,y ) ->Integer.compare(x ,y)

实例

无参数无返回值

package org.example.a;
 
@FunctionalInterface
interface Interface {
    void run();
}
 
public class Demo{
    public static void main(String[] args) {
        Interface params = new Interface() {
            @Override
            public void run() {
                System.out.println("Anonymous Internal Class: ");
            }
        };
 
        Interface params1 = () -> System.out.println("Lambda: ");
 
        params.run();
        params1.run();
    }
}

执行结果

Anonymous Internal Class: 
Lambda: 

有参数无返回值

package org.example.a;
 
@FunctionalInterface
interface Interface {
    void run(String s);
}
 
public class Demo{
    public static void main(String[] args) {
        Interface params = new Interface() {
            @Override
            public void run(String s) {
                System.out.println("Anonymous Internal Class: " + s);
            }
        };
 
        Interface params1 = (s) -> System.out.println("Lambda: " + s);
 
        params.run("hello");
        params1.run("hi");
    }
}

执行结果

Anonymous Internal Class: hello
Lambda: hi 

有参数有返回值

package org.example.a;
 
@FunctionalInterface
interface Interface {
    String run(String s);
}
 
public class Demo{
    public static void main(String[] args) {
        Interface params = new Interface() {
            @Override
            public String run(String s) {
                System.out.println("Anonymous Internal Class: " + s);
                return "abc";
            }
        };
 
        Interface params1 = (s) -> {
            System.out.println("Lambda: " + s);
            return "def";
        };
 
        System.out.println(params.run("hello"));
        System.out.println(params1.run("hi"));
    }
}

执行结果

Anonymous Internal Class: hello
abc
Lambda: hi
def 

lambda作为参数

传递一个函数

package org.example.a;
 
interface IRun {
    String welcome(String string);
}
 
class Util {
    public static long executionTime1(IRun iRun, String string) {
        long startTime = System.currentTimeMillis();
        System.out.println(iRun.welcome(string));
        //本处刻意添加这一无意义延时,防止执行太快返回0
        try {
            Thread.sleep(10);
        } catch (Exception e) {
            System.out.println(e);
        }
        long endTime = System.currentTimeMillis();
        return endTime - startTime;
    }
 
    public long executionTime2(IRun iRun, String string) {
        long startTime = System.currentTimeMillis();
        System.out.println(iRun.welcome(string));
        //本处刻意添加这一无意义延时,防止执行太快返回0
        try {
            Thread.sleep(10);
        } catch (Exception e) {
            System.out.println(e);
        }
        long endTime = System.currentTimeMillis();
        return endTime - startTime;
    }
 
    public static String hello(String string){
        String tmp;
        tmp = "hello: " + string;
        return tmp;
    }
 
    public String hi(String string){
        String tmp;
        tmp = "hi: " + string;
        return tmp;
    }
}
 
public class Demo {
    public static void main(String[] args) {
        long time1 = Util.executionTime1(Util::hello, "Tony");
        long time2 = new Util().executionTime2(new Util()::hi, "Pepper");
        System.out.println("time1: " + time1 + "ms");
        System.out.println("time2: " + time2 + "ms");
    }
}

执行结果

hello: Tony
hi: Pepper
time1: 11ms
time2: 11ms

直接传递lambda函数 

package org.example.a;
 
interface IRun {
    String welcome(String string);
}
 
class Util {
    public static long executionTime(IRun iRun, String string) {
        long startTime = System.currentTimeMillis();
        System.out.println(iRun.welcome(string));
        //本处刻意添加这一无意义延时,防止执行太快返回0
        try {
            Thread.sleep(10);
        } catch (Exception e) {
            System.out.println(e);
        }
        long endTime = System.currentTimeMillis();
        return endTime - startTime;
    }
}
 
public class Demo {
    public static void main(String[] args) {
        long time = Util.executionTime((string -> {
                    String tmp;
                    tmp = "hello: " + string;
                    return tmp;
                })
                , "Tony");
        System.out.println("time: " + time + "ms");
    }
}

执行结果

hello: Tony
time: 11ms

遍历集合

package org.example.a;
 
import java.util.ArrayList;
import java.util.List;
 
public class Demo{
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("aaa");
        list.add("bbb");
 
        //以前的循环方式
        for (String string : list) {
            System.out.println(string);
        }
 
        //使用lambda表达式输出list中的每个值
        list.forEach(c->{
            System.out.println(c);
        });
 
        // 在 Java 8 中使用双冒号操作符(double colon operator)。也属于lamda表达式
        list.forEach(System.out::println);
    }
}

执行结果

aaa
bbb
aaa
bbb
aaa
bbb

创建线程

package org.example.a;
 
public class Demo{
    public static void main(String[] args) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("Anonymous Internal Class !");
            }
        }).start();
 
        new Thread(() -> System.out.println("Lambda !")).start();
    }
}

执行结果

Anonymous Internal Class !
Lambda !

排序

package org.example.a;
 
import java.util.Arrays;
import java.util.Comparator;
 
public class Demo{
    public static void main(String[] args) {
        String[] players = {"Rafael Nadal", "Novak Djokovic", "Stanislas Wawrinka"};
        
        Arrays.sort(players, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return (o1.compareTo(o2));
            }
        });
 
//        Comparator<String> sortByName = (String s1, String s2) -> (s1.compareTo(s2));
//        Arrays.sort(players, sortByName);
        
//        Arrays.sort(players, (String s1, String s2) -> (s1.compareTo(s2)));
        
        for(String string:players){
            System.out.println(string);
        }
    }
}

执行结果(换成注释掉的两种任意一种都是一样的)

Novak Djokovic
Rafael Nadal
Stanislas Wawrinka

The above is the detailed content of How to use Lambda expressions in Java?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete