當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。