[英]How to convert java stream collect to scala
Java code Java代码
objRefs = objRefs.stream().filter(objRef -> DataUtil.isNotEmpty(objRef)).collect(Collectors.toCollection(LinkedHashSet::new));
How to convert it to scala?如何将其转换为scala? Following code compile error以下代码编译错误
objRefs = objRefs.stream().filter(objRef => DataUtil.isNotEmpty(objRef)).collect(Collectors.toCollection(LinkedHashSet::new));
LinkedHashSet::new
is not valid scala. LinkedHashSet::new
不是有效的 scala。 Do you mean to call the constructor of LinkedHashSet?你的意思是调用LinkedHashSet的构造函数吗? If so, use new LinkedHashSet(_)
如果是这样,请使用new LinkedHashSet(_)
The solution you've chosen is translating java to scala.您选择的解决方案是将 java 转换为 Scala。 If you do that, there is little value in using scala at all, and you might as well use java.如果这样做,那么使用 scala 根本没有什么价值,您还不如使用 java。
A more idiomatic solution would be一个更惯用的解决方案是
objRefs.stream().filter(DataUtil.isNotEmpty).asScala(Set)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.