I'm wondering if there is any significant difference between two cases of flatmapping.
Case 1:
someCollection
.stream()
.map(CollectionElement::getAnotherCollection)
.flatMap(Collection::stream);
Case 2:
someCollection
.stream()
.flatMap(element -> element.getAnotherCollection().stream());
Which one should be prefered? Is any of these better in terms of performance?
Which one should be preferred?
The difference is so cosmetic that it's up to you and your team - pick the one that you feel more comfortable with. I'd go for the second option, it's more concise.
Is any of these better in terms of performance?
From the time complexity point of view, no. The first example involves a creation of a few unnecessary objects, so the second one is a more reasonable choice. However, keep in mind that we're talking about micro-optimizations here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With