Java 8 Stream API收集器

编程入门 行业动态 更新时间:2024-10-26 07:36:38
Java 8 Stream API收集器 - addAll不适用于参数Object,对于Object类型,addAll未定义(Java 8 Stream API Collector - addAll not applicable for argument Object, addAll is undefined for type Object)

我的代码没有编译,我不太清楚为什么。 这是代码:

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

My code is not compiling and I'm not quite sure why. Here is the code:

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

And the errors:

(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)

I've also tried Set::add and Set::addAll with pretty much the same result.

EDIT:

I've ended up with this code. Please tell me if there is a cleaner way to do this, or is it ok?

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)

...但坦率地说,用更换整个收藏家创作会更好

toCollection(LinkedHashSet::new)

这相当于。

I think you have the order of the arguments messed up in the first lambda; it should be

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

...though frankly it'd be better just to replace the entire Collector creation with

toCollection(LinkedHashSet::new)

Which is equivalent.

更多推荐

本文发布于:2023-08-04 23:34:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1424181.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:收集器   Java   Stream   API

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!