当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Function Interface用法及代码示例


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

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

The lambda expression assigned to an object of Function type is used to define its apply() which eventually applies the given function on the argument.
 

Methods in Function Interface

Function 接口由以下列出的 4 个方法组成,稍后讨论如下:

  1. apply()
  2. andThen()
  3. compose()
  4. identity()

方法一:apply()

用法:

R apply(T t)

参数:该方法只接受一个参数t这是函数参数

Return Type: 该方法返回函数结果其类型为 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<Integer, Double> half = a -> a / 2.0;
        // Applying the function to get the result
        System.out.println(half.apply(10));
    }
}
输出
5.0

方法二:andThen()

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

用法:

default <V> Function<T, V> 
andThen(Function<? super R, ? extends V> after)

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

参数:该方法接受一个参数after这是在当前函数之后应用的函数。

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

异常:这个方法抛出NullPointerException如果 after 函数为空。

示例 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<Integer, Double> 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<Integer, Double> half = a -> a / 2.0;
        // Try block to check for exceptions
        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 <V> Function<V, R> 
compose(Function<? super V, ? extends T> before)

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

参数:该方法接受一个参数before这是首先应用的函数,然后是当前函数

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

异常:这个方法抛出NullPointerException如果 before 函数为空。

示例 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<Integer, Double> 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<Integer, Double> half = a -> a / 2.0;
        // Try block to 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

方法四:identity()

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

用法:

static <T> Function<T, T> 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<Integer, Integer> i = Function.identity();
        // Print statement
        System.out.println(i.apply(10));
    }
}
输出
10


相关用法


注:本文由纯净天空筛选整理自psil123大神的英文原创作品 Function Interface in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。