Why some collections are modified in java 8 and some are not

I have a question. I saw that some blocks are written like for(element e : elements)e.function(); and some are written like Arrays.stream(elements).forEach(e->e.function()); Witch writing is better ? The other option should be modified?

Hi @andu033, the latter using lambda expressions on collections streams should be preferred wherever Java 8 is supported.

I think we should encourage adopting lambda expressions wherever they make the code more concise and readable. So if you have the opportunity, in the course of the work on a ticket, to bring in lambda expressions where there was a traditional loop, why not do it?

It’s a trade off obviously, lambda expressions reduce verbosity but might be sometimes cryptic, the appropriate choice will have to be made on a case by case basis.

1 Like

I understand now. Thanks for your answer . If i will encounter situations like that i will try to use streams and lambda’s if will make the code more readable . But just to know better . The difference is not big enough to make a PR and modify the block right? :slight_smile:

Your PR would have to be backed by a ticket, so this could happen if there was a design evolution ticket aiming at chasing places where lambda expressions could be brought in.

So the short answer is no :wink:

1 Like

Ok.Thanks for your answer :slight_smile: