简体   繁体   中英

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

截至13版,有transformAndConcatFluentIterable

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

In Eclipse Collections , collect() is the equivalent of Scala's map() , and flatCollect() is the equivalent of Scala's flatMap() .

Here is an example usage of flatCollect with Java 8 Lambdas .

Note: I am a committer for 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. This is a very subtle difference. You need to decide whether the 'view over original collection' is appropriate for your context.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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