如何在Java中使用lambda和方法引用实现ToIntFunction ?
ToIntFunction<T>是 java.util.function包中定义的内置功能接口。该功能接口期望将参数作为输入并产生整数值的结果。它可用作lambda 表达式 或方法 引用的分配目标。ToIntFunction 接口仅容纳一个方法,applyAsInt()。 此方法对给定的参数执行一个运算,并返回整数值的结果。
语法
@FunctionalInterface
public interface ToIntFunction {
int applyAsInt(T value);
}在下面的示例中,我们可以使用lambda表达式和方法引用来实现ToIntFunction<T>。
示例
import java.util.function.ToIntFunction;
import java.time.LocalDate;
public class ToIntFunctionInterfaceTest {
public static void main(String[] args) {
ToIntFunction<Integer> lambdaObj = value -> value * 25; // using lambda expression System.out.println("The result is: " + lambdaObj.applyAsInt(10));
ToIntFunction<String> lenFn = String::length; // using method reference System.out.println("The length of a String is: " + lenFn.applyAsInt("tutorialspoint"));
}
}输出结果
The result is: 250 The length of a String is: 14
热门推荐
10 八一幼儿祝福语大全简短
11 公司乔迁食堂祝福语简短
12 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短