我的代码未编译,我不确定为什么。这是代码:

ArrayList<ClassificationData> classifications = productData
                .stream()
                .filter(p -> CollectionUtils.isNotEmpty(p.getClassifications()))
                .flatMap(p -> p.getClassifications().stream())
                .collect(groupingBy(ClassificationData::getName,
                        mapping(ClassificationData::getFeatures,
                                Collector.of(LinkedHashSet<FeatureData>::new,
                                        (a,b) -> b.addAll(a),
                                        (a,b) -> {
                                            b.addAll(a);
                                            return b;
                                        })
                        )))
                .entrySet()
                .stream()
                .map(e -> {
                    ClassificationData c = new ClassificationData();
                    c.setName(e.getKey());
                    c.setFeatures(e.getValue());
                    return c;
                })
                .collect(Collectors.toCollection(ArrayList::new));


和错误:

    (a,b) -> b.addAll(a),
The method addAll(Collection<? extends FeatureData>) in the type Collection<FeatureData> is not applicable for the arguments (Object)

     b.addAll(a);
The method addAll(Object) is undefined for the type Object

     c.setFeatures(e.getValue());
The method setFeatures(Collection<FeatureData>) in the type ClassificationData is not applicable for the arguments (Object)


我还尝试了Set :: add和Set :: addAll,结果几乎相同。

编辑:

我结束了这段代码。请告诉我是否有更清洁的方法可以执行此操作?

ArrayList<ClassificationData> classifications = productData
                .stream()
                .filter(p -> CollectionUtils.isNotEmpty(p.getClassifications()))
                .flatMap(p -> p.getClassifications().stream())
                .collect(groupingBy(ClassificationData::getName,
                        mapping(ClassificationData::getFeatures,
                                toCollection(LinkedHashSet::new)
                        )))
                .entrySet()
                .stream()
                .map(e -> {
                    ClassificationData c = new ClassificationData();
                    c.setCode(e.getKey());
                    c.setName(e.getKey());
                    c.setFeatures(e.getValue()
                            .stream()
                            .filter(CollectionUtils::isNotEmpty)
                            .flatMap(p -> p.stream())
                            .filter(distinctByKey(FeatureData::getName))
                            .collect(toCollection(ArrayList::new)));
                    return c;
                })
                .collect(toCollection(ArrayList::new));

最佳答案

我认为您在第一个lambda中弄乱了论点的顺序;它应该是

(set, a) -> set.add(a)


...虽然坦率地说,最好将整个Collector创作替换为

toCollection(LinkedHashSet::new)


这是等效的。

关于java - Java 8 Stream API收集器-addAll不适用于参数Object,addAll对于Object类型未定义,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40072975/

10-10 07:16