📜  Java – Lambda 表达式参数

📅  最后修改于: 2022-05-13 01:54:22.250000             🧑  作者: Mango

Java – Lambda 表达式参数

Lambda 表达式是匿名函数。这些函数不需要使用名称或类。 Java 8 中增加了 Lambda 表达式。 Lambda 表达式基本上表达了函数式接口的实例 具有单个抽象方法的接口称为函数式接口。一个例子是Java.lang.Runnable。

Lambda 表达式只实现了一个抽象函数,因此实现了函数式接口。谓词接口是一个函数式接口的例子,它只有一个叫做 test() 的抽象方法。

插图:

interface Predicate
{
    ......
    abstract boolean test(T t)
}

上面是一个函数式接口,它有一个抽象方法 test 只接收一个 T 类型的参数并返回一个布尔值。此方法是一种采用类型参数的泛型方法。可以使用 lambda 表达式在程序中的任何位置实现此接口,而不是创建具有多个函数的类。例如,要实现一个仅用于多线程的可运行接口,只需实现一个 run() 方法。然后是可以使用 compare() 方法实现的可比较接口。

要点:



  • lambda 表达式的主体可以包含零个、一个或多个语句。
  • 当有单个语句时,大括号不是强制性的,匿名函数的返回类型与主体表达式的返回类型相同。
  • 当有多个语句时,则必须将它们括在大括号中(一个代码块),并且匿名函数的返回类型与代码块内返回值的类型相同,如果没有返回,则为void .

这些是单-其返回类型为void行lambda表达式。

类型 1:无参数。

句法:

() -> System.out.println("Hello");

它采用以下形式的接口:

interface Test1
{
    void print()
}

类型 2:单一参数。

句法:

(p) -> System.out.println(p);

如果可以从上下文推断该变量的类型,则不必使用括号



它采用以下形式的接口:

interface Test2
{
    void print(Integer p)
}

lamda 的类型和返回类型是自动推断的。

类型 3:多参数

(p1, p2) -> System.out.println(p1 + " " + p2);

如果可以从上下文推断该变量的类型,则不必使用括号

它采用以下形式的接口:

interface Test3
{
    void print(Integer p1, Integer p2)
}

lamda 的类型和返回类型是自动推断的。

现在,我们已经完成了对理论概念的讨论,现在让我们提出实现部分。所以这里主要我们将讨论上面讨论的上述三种类型的代码:

因此,如果我们确实得出上述结论

示例 1:不带参数的 Lambda 表达式

Java
// Java code to illustrate lambda expression
// without parameters
  
// functional interface
// without parameters
interface Test1 {
    void print();
}
  
class GfG {
    // functional interface parameter is passed
    static void fun(Test1 t) { t.print(); }
    public static void main(String[] args)
    {
        // lambda expression is passed
        // without parameter to functional interface t
        fun(() -> System.out.println("Hello"));
    }
}


Java
// Java code to illustrate lambda expression
// with single parameter
  
// functional interface
// with one parameter of Integer type
interface Test2 {
    // The void type and the Integer type
    // is automatically inferred from here
    // and assigned to the lambda expression
    void print(Integer p);
}
  
class GfG {
    // takes lambda expression and a variable of
    // Integer type as arguments
    static void fun(Test2 t, Integer p)
    {
        // calls the print function
        t.print(p);
    }
    public static void main(String[] args)
    {
        // lambda expression is passed
        // with a single parameter
        // lambda expression is mapped to the
        // single argument abstract function in the
        // functional interface Test2
        fun(p -> System.out.println(p), 10);
    }
}


Java
// Java code to illustrate lambda expression
// with multi parameters
  
// functional interface Test3
// with 2 parameter of Integer type
interface Test3 {
    // The void type and the Integer type
    // is automatically inferred from here
    // and assigned to the lambda expression
    void print(Integer p1, Integer p2);
}
  
class GfG {
    // takes parameter of Test3 type followed
    // by 2 integer parameters p1 and p2
    static void fun(Test3 t, Integer p1, Integer p2)
    {
        // calls the print function
        t.print(p1, p2);
    }
    public static void main(String[] args)
    {
        // lambda expression is passed
        // with two parameters
        // lambda expression is mapped to the
        // double argument abstract function in the
        // functional interface Test3
        fun((p1, p2)
                -> System.out.println(p1 + " " + p2),
            10, 20);
    }
}


输出
Hello

示例 2:带有单个参数的类型 2 Lambda 表达式

Java

// Java code to illustrate lambda expression
// with single parameter
  
// functional interface
// with one parameter of Integer type
interface Test2 {
    // The void type and the Integer type
    // is automatically inferred from here
    // and assigned to the lambda expression
    void print(Integer p);
}
  
class GfG {
    // takes lambda expression and a variable of
    // Integer type as arguments
    static void fun(Test2 t, Integer p)
    {
        // calls the print function
        t.print(p);
    }
    public static void main(String[] args)
    {
        // lambda expression is passed
        // with a single parameter
        // lambda expression is mapped to the
        // single argument abstract function in the
        // functional interface Test2
        fun(p -> System.out.println(p), 10);
    }
}
输出
10

示例 3:具有多参数的 Type 3 Lambda 表达式

Java

// Java code to illustrate lambda expression
// with multi parameters
  
// functional interface Test3
// with 2 parameter of Integer type
interface Test3 {
    // The void type and the Integer type
    // is automatically inferred from here
    // and assigned to the lambda expression
    void print(Integer p1, Integer p2);
}
  
class GfG {
    // takes parameter of Test3 type followed
    // by 2 integer parameters p1 and p2
    static void fun(Test3 t, Integer p1, Integer p2)
    {
        // calls the print function
        t.print(p1, p2);
    }
    public static void main(String[] args)
    {
        // lambda expression is passed
        // with two parameters
        // lambda expression is mapped to the
        // double argument abstract function in the
        // functional interface Test3
        fun((p1, p2)
                -> System.out.println(p1 + " " + p2),
            10, 20);
    }
}
输出
10 20