1.JDK中函数式接口的包路径
包路径:java.util.function
2.函数式接口常用示例
2.1 函数式接口基础练习
package lambda;
import java.util.function.*;
public class FunctionalInterfacePractice {
public static void main(String[] args) {
FunctionalInterfacePractice info = new FunctionalInterfacePractice();
/*void返回值场景1:输入参数0个,返回值void*/
Runnable runnable = () -> info.justDoWithoutAnyInput();
runnable.run();
/*void返回值场景2:输入参数1个,返回值void*/
Consumer<String> consumer = e -> info.justDoWithOneInput(e);
consumer.accept("input_1");
/*void返回值场景3:输入参数2个,返回值void*/
BiConsumer<String, Double> biConsumer = (e, f) -> info.justDoWithTwoInput(e, f);
biConsumer.accept("myStr", 1.2345);
/*void返回值场景4(自我扩展):输入参数3个,返回值void 可以自定义带3个参数的函数式接口。*/
ThreeConsumer<String, Double, Integer> threeConsumer = (e, v, f) -> info.justDoWithThreeInput(e, v, f);
threeConsumer.accept("3x", 1.3, 22);
/*指定返回值场景1:输入参数0个,返回值指定类型*/
Supplier<String> supplier = () -> info.returnWithoutAnyInput();
String rtn1 = supplier.get();
System.out.println(rtn1);
/*指定返回值场景2:输入参数1个,返回值指定类型*/
Function<Double, String> function = e -> info.returnWithOneInput(e);
String rtn2 = function.apply(123.123);
System.out.println(rtn2);
/*指定返回值场景3:输入参数2个,返回值指定类型*/
BiFunction<Integer, String, Integer> biFunction = (t, u) -> info.returnWithTwoInput(t, u);
Integer rtn3 = biFunction.apply(123, "123");
System.out.println(rtn3);
/*指定返回值场景4(自我扩展):输入参数3个,返回值指定类型。可以自定义带3个参数的函数式接口。*/
ThreeFunction<Integer, String, Double, String> threeFunction = (a, b, c) -> info.returnWithThreeInput(a, b, c);
String rtn4 = threeFunction.apply(11, ":1:1:", 1.111);
System.out.println(rtn4);
}
private String returnWithThreeInput(Integer a, String b, Double c) {
return String.join(",", a.toString(), b, c.toString());
}
private Integer returnWithTwoInput(Integer t, String u) {
return t.intValue() + Integer.valueOf(u);
}
private String returnWithOneInput(Double e) {
return "returnWithOneInput:" + e.toString();
}
private void justDoWithThreeInput(String e, Double v, Integer f) {
System.out.println(String.format("justDoWithThreeInput: %s,%.4f,%d", e, v, f));
}
private void justDoWithTwoInput(String e, Double f) {
System.out.println(String.format("justDoWithTwoInput: %s,%.4f", e, f));
}
private String returnWithoutAnyInput() {
String rtn = "returnWithoutAnyInput: return String";
System.out.println(rtn);
return rtn;
}
private void justDoWithOneInput(String param1) {
System.out.println("justDoWithOneInput: " + param1);
}
private void justDoWithoutAnyInput() {
System.out.println("justDoWithoutAnyInput");
}
@FunctionalInterface //这行加不加都行,加了更醒目一点是函数式接口,建议加一下
public interface ThreeConsumer<T, V, F> {
void accept(T t, V v, F f);
}
@FunctionalInterface//这行加不加都行,加了更醒目一点是函数式接口,建议加一下
public interface ThreeFunction<T, U, V, R> {
R apply(T t, U u, V v);
}
}
2.2 使用函数式接口的lambda表达式重构代码
当判断分支很多的时候testLambda方法的形式的代码可以更加简洁,testTraditional方法的方式就会显得复杂度较高。
package lambda;
import java.util.HashMap;
import java.util.Map;
import java.util.function.*;
public class FunctionalInterfacePractice {
public static void main(String[] args) {
/*利用函数式接口的lambda表达式进行代码重构*/
testLambda("justDoWithTwoInput");
testTraditional("justDoWithTwoInput");
}
public static void testLambda(String methodName) {
Map<String, BiConsumer<String, Double>> map = new HashMap<>();
FunctionalInterfacePractice info = new FunctionalInterfacePractice();
map.put("justDoWithoutAnyInput", (a, b) -> info.justDoWithoutAnyInput());
map.put("justDoWithOneInput", (a, b) -> info.justDoWithOneInput(a));
map.put("justDoWithTwoInput", (a, b) -> info.justDoWithTwoInput(a, b));
map.get(methodName).accept("test::123", 1.12);
}
public static void testTraditional(String methodName) {
FunctionalInterfacePractice info = new FunctionalInterfacePractice();
if(methodName.equals("justDoWithoutAnyInput")) {
info.justDoWithoutAnyInput();
}
else if(methodName.equals("justDoWithOneInput")) {
info.justDoWithOneInput("test::123");
}
else if(methodName.equals("justDoWithTwoInput")) {
info.justDoWithTwoInput("test::123", 1.12);
}
else {
//do nothing
}
}
private void justDoWithTwoInput(String e, Double f) {
System.out.println(String.format("justDoWithTwoInput: %s,%.4f", e, f));
}
private void justDoWithOneInput(String param1) {
System.out.println("justDoWithOneInput: " + param1);
}
private void justDoWithoutAnyInput() {
System.out.println("justDoWithoutAnyInput");
}
}
3.JDK中现成的函数式接口