将 Map 对象缩减为 ArrayList

Reduce Map object into ArrayList

我有一个由 4 个工人组成的分布式系统,每个工人都发送 1 个 Map 对象到 reducer,以便将它们缩减为 ArrayList< MyClass >

我试图解决这个问题的方法是将所有 Map 项从 worker 收集到一个 ArrayList 中,然后使用并行流我想使用 reduce 方法将它们缩减到一个列表中,我已经初始化到减速器。这是我的代码:

    ArrayList<MyClass> result = new ArrayList<MyClass>();
    maps.parallelStream().reduce(null, (res, m)->{
        result.add(m.get(key));
    });

我在 reduce 方法上遇到以下编译错误:

The method reduce(Map< String,Directions>, BinaryOperator< Map< String,Directions>>) in the type Stream< Map< String,Directions > > is not applicable for the arguments (null, (< no type > res, < no type > m) -> {})

假设 list 是您的地图列表,请尝试以下操作:

List<MyClass> result = list.stream()
    .flatMap(v -> v.values().stream()).collect(Collectors.toList());