s-day07--常用的函数式接口-Function

This commit is contained in:
xx
2026-10-20 13:53:14 +08:00
parent 7165e6cd08
commit 42e38e7059
@@ -0,0 +1,31 @@
package com.inmind.functional_interface02;
import java.util.function.Function;
/*
17.常用的函数式接口-Function
Interface Function<T,R>:就是一个数据工厂,用来数据转换
参数类型:
T:原料的类型--参数
R:产品的类型--返回值
抽象方法:
R apply(T t) 将此函数应用于给定的参数。
//需求:"100"--->100【直接使用包装类parseXXX方法即可】
T:String
R:Integer
*/
public class Demo07 {
public static void main(String[] args) {
String s = "100";
method((String str)->{
return Integer.parseInt(str)*2;
},s);
}
//定义接收一个字符串,使用数据工厂Function,将字符串100转为整数100
public static void method(Function<String, Integer> function, String str) {
Integer result = function.apply(str);
System.out.println(result);
}
}