📜  Java中的函数接口与示例

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

Java中的函数接口与示例

函数接口Java .util 的一部分。从Java 8 开始引入的函数包,用于在Java中实现函数式编程。它表示一个接受一个参数并产生结果的函数。因此,此功能接口采用 2 个泛型,如下所示:

  • T :表示输入参数的类型
  • R : 表示函数的返回类型

函数接口中的方法

函数接口由下面列出的 4 个方法组成,稍后将讨论如下:

  1. 申请()
  2. 进而()
  3. 撰写()
  4. 身份()

方法一: apply()

句法:

R apply(T t)

参数:此方法只接受一个参数t ,即函数参数

返回类型:该方法返回R类型的函数结果

例子

Java
// Java Program to Illustrate Functional Interface
// Via apply() method
 
// Importing interface
import java.util.function.Function;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String args[])
    {
        // Function which takes in a number
        // and returns half of it
        Function half = a -> a / 2.0;
 
        // Applying the function to get the result
        System.out.println(half.apply(10));
    }
}


Java
// Java Program to illustrate addThen() method
 
// Importing interface
import java.util.function.Function;
 
// Main class
public class GFG {
 
    // main driver method
    public static void main(String args[])
    {
        // Function which takes in a number and
        // returns half of it
        Function half = a -> a / 2.0;
 
        // Now treble the output of half function
        half = half.andThen(a -> 3 * a);
 
        // Applying the function to get the result
        // and printing on console
        System.out.println(half.apply(10));
    }
}


Java
// Java Program to illustrate addThen() method
// When NullPointerException occurs
 
// Importing interface
import java.util.function.Function;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String args[])
    {
 
        // Function which takes in a number and
        // returns half of it
        Function half = a -> a / 2.0;
 
        // Try block to check for exce3ptions
        try {
 
            // Trying to pass null as parameter
            half = half.andThen(null);
        }
 
        // Catch block to handle exceptions
        catch (Exception e) {
 
            // Print statement
            System.out.println("Exception thrown "
                               + "while passing null: "
                               + e);
        }
    }
}


Java
// Java Program to illustrate compose() method
 
// Importing Function interface
import java.util.function.Function;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String args[])
    {
        // Function which takes in a number and
        // returns half of it
        Function half = a -> a / 2.0;
 
        // However treble the value given to half function
        half = half.compose(a -> 3 * a);
 
        // Applying the function to get the result
        System.out.println(half.apply(5));
    }
}


Java
// Java Program to illustrate compose() method
 
// Importing Function interface
import java.util.function.Function;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String args[])
    {
        // Function which takes in a number and
        // returns half of it
        Function half = a -> a / 2.0;
 
        // Try bloc kto check for exceptions
        try {
 
            // Trying to pass null as parameter
            half = half.compose(null);
        }
 
        // Catch block to handle exceptions
        catch (Exception e) {
 
            // Print statement
            System.out.println("Exception thrown "
                               + "while passing null: "
                               + e);
        }
    }
}


Java
// Java Program to Illustrate identity() method
 
// Importing Function interface
import java.util.function.Function;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String args[])
    {
        // Function which takes in a number and
        // returns it
        Function i = Function.identity();
 
        // Print statement
        System.out.println(i.apply(10));
    }
}


输出
5.0

方法二: andThen()

它返回一个组合函数,其中参数化函数将在第一个函数之后执行。如果任一函数的评估引发错误,则会将其转发给组合函数的调用者。

句法:

default  Function 
andThen(Function after)

其中V是 after函数和组合函数的输出类型

参数:此方法接受一个参数,其后是要在当前参数之后应用的函数。\

返回值:此方法返回一个组合函数,该函数先应用当前函数,然后应用后函数

异常:如果 after函数为 null,则此方法抛出NullPointerException

示例 1:

Java

// Java Program to illustrate addThen() method
 
// Importing interface
import java.util.function.Function;
 
// Main class
public class GFG {
 
    // main driver method
    public static void main(String args[])
    {
        // Function which takes in a number and
        // returns half of it
        Function half = a -> a / 2.0;
 
        // Now treble the output of half function
        half = half.andThen(a -> 3 * a);
 
        // Applying the function to get the result
        // and printing on console
        System.out.println(half.apply(10));
    }
}
输出
15.0

示例 2:演示何时返回 NullPointerException。

Java

// Java Program to illustrate addThen() method
// When NullPointerException occurs
 
// Importing interface
import java.util.function.Function;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String args[])
    {
 
        // Function which takes in a number and
        // returns half of it
        Function half = a -> a / 2.0;
 
        // Try block to check for exce3ptions
        try {
 
            // Trying to pass null as parameter
            half = half.andThen(null);
        }
 
        // Catch block to handle exceptions
        catch (Exception e) {
 
            // Print statement
            System.out.println("Exception thrown "
                               + "while passing null: "
                               + e);
        }
    }
}
输出
Exception thrown while passing null: java.lang.NullPointerException

方法三: compose()

它返回一个组合函数,其中参数化函数将首先执行,然后是第一个。如果任一函数的评估引发错误,则会将其转发给组合函数的调用者。

句法:

default  Function 
compose(Function before)

其中 V 是前函数和组合函数的输入类型

参数:此方法接受一个参数,在该参数之前是要应用的函数,然后是当前函数

返回值:此方法返回一个组合函数,该函数在参数化函数之后应用当前函数

异常:如果 before函数为 null,则此方法抛出 NullPointerException。

示例 1:

Java

// Java Program to illustrate compose() method
 
// Importing Function interface
import java.util.function.Function;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String args[])
    {
        // Function which takes in a number and
        // returns half of it
        Function half = a -> a / 2.0;
 
        // However treble the value given to half function
        half = half.compose(a -> 3 * a);
 
        // Applying the function to get the result
        System.out.println(half.apply(5));
    }
}
输出
7.5

示例 2:返回 NullPointerException 时。

Java

// Java Program to illustrate compose() method
 
// Importing Function interface
import java.util.function.Function;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String args[])
    {
        // Function which takes in a number and
        // returns half of it
        Function half = a -> a / 2.0;
 
        // Try bloc kto check for exceptions
        try {
 
            // Trying to pass null as parameter
            half = half.compose(null);
        }
 
        // Catch block to handle exceptions
        catch (Exception e) {
 
            // Print statement
            System.out.println("Exception thrown "
                               + "while passing null: "
                               + e);
        }
    }
}
输出
Exception thrown while passing null: java.lang.NullPointerException

方法4:身份()

此方法返回一个返回其唯一参数的函数。

句法:

static  Function identity()

其中T表示参数的类型和要返回的值

返回:此方法返回一个函数,该函数返回自己的参数

例子

Java

// Java Program to Illustrate identity() method
 
// Importing Function interface
import java.util.function.Function;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String args[])
    {
        // Function which takes in a number and
        // returns it
        Function i = Function.identity();
 
        // Print statement
        System.out.println(i.apply(10));
    }
}
输出
10