有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java泛型中的动态数据类型

我有一个客户端发送列表的用例<;功能>;。任务是迭代并执行此函数,并将其保存在TypedSafeMap中。 伪客户端代码:

Function<String, Integer> firstFn = x -> x.length();
Function<String, String> secondFn = x -> x.substring(0);

client.runTheseFunctions(Arrays.asList(firstFn, secondFn));

在代码中的runtTheseFunctions中,任务是执行这些函数,并将其保存在TypedSafeMap中,其中键是函数结果类型的数据类型,值是函数的返回。应用()

下面的代码

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.
           });
    }

共 (0) 个答案