首页 > 解决方案 > 如何运行作为地图上的值的 lambda 表达式

问题描述

我对java很陌生,我正在尝试创建一组通过映射中的lambda表达式获取的对象。基本上,我从映射(lambda 表达式)中获取一个值并运行它以获得一个布尔值。但是,在表达式上运行 .apply 时出现错误。有想法该怎么解决这个吗?任何帮助表示赞赏。

        Map<String, Predicate<IndexSub>> order_function = new HashMap<>();
        order_function.put("AlternativesValues", x -> false);
        order_function.put("AlternativesConstituent", x -> x.getCloseCons());
        order_function.put("EquityValues", x -> false);
        order_function.put("EquityCloseConstituent", x -> x.getCloseCons());
        order_function.put("EquityOpenConstituent", x -> x.getOpenCons());
        order_function.put("FixedValues", x -> false);
        order_function.put("FixedReturns", x -> x.getCloseCons());
        order_function.put("FixedStatistics", x -> x.getOpenCons());

        //getCloseCons and getOpenCons return true/false    

        Set<String> orderable_sub = new HashSet<String>();

        for (IndexSub s : tenant_subscriptions) {
                                 //DataProduct is a string
            if (order_function.get(DataProduct).apply(s) == true){
                orderable_sub.add(s.getIndexId());
            }

        }

标签: javalambdamaps

解决方案


Predicate功能接口有test()方法,而不是apply()

if (order_function.get(DataProduct).test(s)){
    orderable_sub.add(s.getIndexId());
}

推荐阅读