凡事都在事上磨,这样才能精进,成长进步提高! ------ 博客园首页

函数式接口验证

package com.xlg.task.common;

import java.util.HashMap;

public class FunctionalInterfaceTest {
public static void main(String[] args) {
int x = 1;
int y = 2;
String key = "2";
HashMap<String, NoParamConsume> map = new HashMap<>();
map.put("1", () -> get1(x, y));
map.put("2", () -> get2(2, 2, 3));
map.forEach((k, v) -> {
if (k.equals(key)) {
System.out.println(v.apply());
}
});
}

private static Integer get2(Integer x, Integer y, Integer z) {
return 2 * x + y + z;
}

private static Integer get1(Integer x, Integer y) {
return 2 * x + 1 + y;
}

}

@FunctionalInterface
interface NoParamConsume<R> {
R apply();
}
posted @ 2024-05-06 23:43  追风fc  阅读(21)  评论(0)    收藏  举报