Java中的函数接口及示例

这个 功能接口 是这个世界的一部分 JAVAutil。作用 从Java 8开始引入的包,用于实现 函数式编程 在爪哇。它表示接受一个参数并产生结果的函数。因此,该功能接口包含两个泛型,即:

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

分配给函数类型的对象的lambda表达式用于定义其 应用() 最终将给定函数应用于参数。

函数接口中的方法

功能界面由以下4种方法组成,如下所示:

  1. 应用()
  2. 然后()
  3. 撰写
  4. 身份()

方法1: 应用()

语法:

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<Integer, Double> half = a -> a / 2.0 ;
// Applying the function to get the result
System.out.println(half.apply( 10 ));
}
}


输出

5.0

方法2: 然后()

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

语法:

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

哪里 五、 是after函数和组合函数的输出类型

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

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

例外情况: 这个方法抛出 空指针异常 如果after函数为null。

例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 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

方法3: 撰写

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

语法:

default <V> Function<V, R> compose(Function<? super V, ? extends T> before)

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

参数: 此方法接受一个参数 之前 首先应用哪个函数,然后是当前函数

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

例外情况: 这个方法抛出 空指针异常 如果before函数为null。

例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 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 <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

© 版权声明
THE END
喜欢就支持一下吧
点赞13 分享