【发布时间】:2021-07-28 14:09:47
【问题描述】:
我有一个用例,客户端发送列表
Function<String, Integer> firstFn = x -> x.length();
Function<String, String> secondFn = x -> x.substring(0);
client.runTheseFunctions(Arrays.asList(firstFn, secondFn));
在代码中的runtTheseFunctions内部,任务是执行这些函数并将其保存在一个TypedSafeMap中,其中key是函数结果类型的数据类型,value是functions.apply()的返回值;
下面的代码
public static void runTheseFunctions(List<Function<Employee, ?>> lst, Employee o) {
lst.stream().forEach( x -> {
typedSafeMap.put(????, x.apply(o));
//The key is nothing but the datatype of the x.apply(o).
//How do I add this in runtime here. Generics is all compile time safety.
});
}
【问题讨论】:
标签: java generics java-8 functional-programming