Java |双功能接口方法–apply()和addThen()

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

null

因此,该功能接口包含3个参数,即:-

  • T :表示函数的第一个参数的类型
  • U :表示函数的第二个参数的类型
  • R :表示函数的返回类型

分配给双函数类型对象的lambda表达式用于定义其 应用() 最终将给定函数应用于参数。使用双函数的主要优点是,它允许我们使用2个输入参数,而在函数中只能有1个输入参数。

双功能接口中的函数

双功能界面由以下两个功能组成:

1.申请

此方法将给定函数应用于参数。

语法:

R apply(T t, U u)

参数: 此方法采用两个参数:

  • T –第一个函数参数
  • U –第二个函数参数

返回: 此方法返回类型为R的函数结果。

下面是说明apply()方法的代码:

项目1:

// Java Program to demonstrate
// BiFunction's apply() method
import java.util.function.BiFunction;
public class Main {
public static void main(String args[])
{
// BiFunction to add 2 numbers
BiFunction<Integer, Integer, Integer> add = (a, b) -> a + b;
// Implement add using apply()
System.out.println( "Sum = " + add.apply( 2 , 3 ));
// BiFunction to multiply 2 numbers
BiFunction<Integer, Integer, Integer> multiply = (a, b) -> a * b;
// Implement add using apply()
System.out.println( "Product = " + multiply.apply( 2 , 3 ));
}
}


输出:

Sum = 5
Product = 6

2.addThen()

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

注: 作为参数传递的函数应该是function类型,而不是BiFunction类型。

语法:

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

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

参数: 此方法接受一个参数 之后 在这个函数为1之后应用哪个函数。

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

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

下面是演示addThen()方法的代码:

项目1:

// Java Program to demonstrate
// BiFunction's addThen() method
import java.util.function.BiFunction;
public class Main {
public static void main(String args[])
{
// BiFunction to demonstrate composite functions
// Here it will find the sum of two integers
// and then return twice their sum
BiFunction<Integer, Integer, Integer> composite1 = (a, b) -> a + b;
// Using addThen() method
composite1 = composite1.andThen(a -> 2 * a);
// Printing the results
System.out.println( "Composite1 = " + composite1.apply( 2 , 3 ));
// BiFunction to demonstrate composite functions
// Here it will find the sum of two integers
// and then return twice their sum
BiFunction<Integer, Integer, Integer> composite2 = (a, b) -> a * b;
// Using addThen() method
composite2 = composite2.andThen(a -> 3 * a);
// Printing the result
System.out.println( "Composite2 = " + composite2.apply( 2 , 3 ));
}
}


输出:

Composite1 = 10
Composite2 = 18

项目2: 演示何时返回NullPointerException。

// Java Program to demonstrate
// BiFunction's addThen() method
import java.util.function.BiFunction;
public class Main {
public static void main(String args[])
{
// BiFunction which finds the sum of 2 integers
// and returns twice their sum
BiFunction<Integer, Integer, Integer> composite = (a, b) -> a + b;
try {
// Using addThen() method
composite = composite.andThen( null );
// Printing the result
System.out.println( "Composite = " + composite.apply( 2 , 3 ));
}
catch (Exception e) {
System.out.println( "Exception: " + e);
}
}
}


输出:

Exception: java.lang.NullPointerException

方案3: 演示如何返回和处理after函数中的异常。

在下面的程序中,当(2,3)作为参数传递给第一个函数时,它返回和5。现在,这个总和将作为参数传递给after函数,即addThen()方法。在这里,将5传递给after函数将得到(5–5=0),即分母将变为0。因此将抛出算术异常。此异常将在apply()函数中处理,而不是在addThen()函数中处理。

// Java Program to demonstrate
// BiFunction's addThen() method
import java.util.function.BiFunction;
public class Main {
public static void main(String args[])
{
// BiFunction which finds the sum of 2 integers
// and returns twice their sum
BiFunction<Integer, Integer, Integer> composite = (a, b) -> a + b;
// Using addThen() method
composite = composite.andThen(a -> a / (a - 5 ));
try {
// Printing the result using apply()
System.out.println( "Composite = " + composite.apply( 2 , 3 ));
}
catch (Exception e) {
System.out.println( "Exception: " + e);
}
}
}


输出:

Exception: java.lang.ArithmeticException: / by zero

注:

  • 无法使用addThen()将双函数添加到现有的双函数中。
  • 当需要传递两个参数时,双功能接口非常有用,而函数接口只允许传递一个参数。但是,可以级联两个或多个函数对象以形成一个双函数,但在这种情况下,不可能同时使用这两个参数。这就是双功能的实用性。
  • Lambda表达式用于初始化双函数接口中的apply()方法。
© 版权声明
THE END
喜欢就支持一下吧
点赞7 分享