简体   繁体   English

什么是番石榴相当于Scala的flatMap?

[英]What is the Guava equivalent of Scala's flatMap?

Looking through https://github.com/google/guava/wiki/FunctionalExplained I see operations like transform , which will transform a list but keep the same cardinality. 通过https://github.com/google/guava/wiki/FunctionalExplained查看我看到像transform这样的操作,它将转换列表但保持相同的基数。 How can I perform a transform that results in a different cardinality? 如何执行导致不同基数的转换? eg (pseudocode) List(1,2,3).transform(i => List(i, i)) -> List(1,1,2,2,3,3) 例如(伪代码) List(1,2,3).transform(i => List(i, i)) - > List(1,1,2,2,3,3)

截至13版,有transformAndConcatFluentIterable

我不认为有直接的翻译,但是concat后面的transform应该是等价的。

In Eclipse Collections , collect() is the equivalent of Scala's map() , and flatCollect() is the equivalent of Scala's flatMap() . Eclipse Collections中collect()相当于Scala的map() ,而flatCollect()相当于Scala的flatMap()

Here is an example usage of flatCollect with Java 8 Lambdas . 以下是使用Java 8 Lambdas的flatCollect的示例用法。

Note: I am a committer for Eclipse Collections. 注意:我是Eclipse Collections的提交者。

Pay attention to one thing, though: Scala's map methods return new collections, while the transform() method in Guava returns a transformed view of the original collection. 但要注意一件事:Scala的map方法返回新的集合,而Guava中的transform()方法返回原始集合的转换视图。 This is a very subtle difference. 这是一个非常微妙的区别。 You need to decide whether the 'view over original collection' is appropriate for your context. 您需要确定“查看原始集合”是否适合您的上下文。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM