Its very hard to come-up with a hard & fast rule on when to use Stream API/Lambda or traditional loops. However, in the Carbon platform we have come-up with a rule of thumb for such scenario.
*Always give the priority to code readability*. It doesn't matter whether you use Streams, Lambda, or traditional loops, always write clean, readable, self-descriptive code. *Only exception is performance*. If your code is performance critical, then you are allowed to do optimizations that might lead to a more complex code. Even in such situation, always try to maintain the code readability at a high level (comments can be a good tool here). Why Code Readability Matters? [1] [1] https://dzone.com/articles/why-code-readability-matters Thanks. On Fri, Apr 7, 2017 at 12:16 PM, Vidura Nanayakkara <[email protected]> wrote: > Hi, > > Blog [1] <http://blog.codefx.org/java/stream-performance/> compares > streams and loops for arrays and lists for different types of operations > and is worth taking a look at. Blog [2] > <https://blog.oio.de/2016/01/22/parallel-stream-processing-in-java-8-performance-of-sequential-vs-parallel-stream-processing/> > discusses > when to use parallel streams which are also worth taking a look. IMO this > depends according to the scenario you are left to deal with. For instance, > in Carbon UUF we use loops instead of streams since the datasets we > consider are relatively small (UUF is performance critical). There are also > instances where we use streams where the possible performance drop due to > using streams is small considering the code readability (the code is > complex when loops are used). > > [1] Loops vs Streams <http://blog.codefx.org/java/stream-performance/> > [2] Parallel stream processing in Java 8 – performance of sequential vs. > parallel stream processing > <https://blog.oio.de/2016/01/22/parallel-stream-processing-in-java-8-performance-of-sequential-vs-parallel-stream-processing/> > [3] What's Wrong in Java 8, Part III: Streams and Parallel Streams > <https://dzone.com/articles/whats-wrong-java-8-part-iii> > > On Fri, Apr 7, 2017 at 9:02 AM, Jude Niroshan <[email protected]> > wrote: > >> I agree with Tharika. It's more important to know that streams can be >> considered when you need parallel processing done. Being said that, usage >> has to be careful about the mutable operations. AFAIK, streams are good at >> map reduction over the traditional for loops. It is more easy to manipulate >> the collections. But it is not advisable to always go for streams. >> >> On Thu, Apr 6, 2017 at 8:54 PM, Pumudu Ruhunage <[email protected]> wrote: >> >>> Hi, IMO for less number of iterations/elements for loops are much >>> efficient, and when number gets higher both are performing with similar >>> efficiency. Advantage of parallel streams shows up when number of elements >>> increases and cost of each iteration increases. >>> Therefore, usage of streams/loops is highly dependent of the scenario >>> and can not suggest either without proper knowledge of the scenario, >>> especially for critical paths. >>> >>> Regards, >>> >>> On Thu, Apr 6, 2017 at 5:19 PM, Tharika Madurapperuma <[email protected]> >>> wrote: >>> >>>> Hi Rajith, >>>> >>>> IMO Java streams should not be used always as a practice. It is not >>>> a silver bullet. There can be issues in performance in some cases as you >>>> have pointed out. >>>> >>>> One of the main advantage of using streams is when the logic of >>>> looping is quite complex and involves different operations on collections >>>> together, so that it can improve readability and understandability. But at >>>> the same time it can result in new and subtle bugs if we are not careful >>>> enough [1]. >>>> >>>> The performance aspect of streams vs loops depends on the type of >>>> the collection and the situation they deal with. This performance >>>> difference can sometimes be negligible. Hence we cannot completely go with >>>> one against another. >>>> >>>> So IMHO we can consider using streams only when necessary based on >>>> our requirement and use loops where it deserves. >>>> >>>> >>>> [1] https://blog.jooq.org/2014/06/13/java-8-friday-10-subtle-mis >>>> takes-when-using-the-streams-api/ >>>> >>>> >>>> Thanks, >>>> Tharika. >>>> >>>> >>>> On Thu, Mar 30, 2017 at 12:01 PM, Rajith Roshan <[email protected]> >>>> wrote: >>>> >>>>> Hi all, >>>>> >>>>> Most of our C5 development have used java8 streams api frequently more >>>>> than the for loops. >>>>> But there are some research have done to compare the performance of >>>>> streams api compared to for loops. They have shown that stream API is slow >>>>> compared to for loops in many scenarios (even for parallel streams). >>>>> [1][2] >>>>> >>>>> Is there any proper guidance to use which one over the other or are we >>>>> using streams as a practice in C5 development >>>>> >>>>> [1] - https://blog.jooq.org/2015/12/08/3-reasons-why-you-shouldn >>>>> t-replace-your-for-loops-by-stream-foreach/ >>>>> [2] - https://jaxenter.com/java-performance-tutorial-how-fast-ar >>>>> e-the-java-8-streams-118830.html >>>>> >>>>> Thanks! >>>>> Rajith >>>>> >>>>> -- >>>>> Rajith Roshan >>>>> Software Engineer, WSO2 Inc. >>>>> Mobile: +94-72-642-8350 <%2B94-71-554-8430> >>>>> >>>>> _______________________________________________ >>>>> Dev mailing list >>>>> [email protected] >>>>> http://wso2.org/cgi-bin/mailman/listinfo/dev >>>>> >>>>> >>>> >>>> >>>> -- >>>> *Tharika Madurapperuma* >>>> Software Engineer | WSO2, Inc. >>>> >>>> Email : [email protected] >>>> Mobile : +94777875624 <+94%2077%20787%205624> >>>> Web : http://wso2.com >>>> >>>> <http://wso2.com/signature> >>>> >>>> _______________________________________________ >>>> Dev mailing list >>>> [email protected] >>>> http://wso2.org/cgi-bin/mailman/listinfo/dev >>>> >>>> >>> >>> >>> -- >>> Pumudu Ruhunage >>> Software Engineer | WSO2 Inc >>> M: +94 779 664493 | http://wso2.com >>> <https://wso2.com/signature> >>> >>> _______________________________________________ >>> Dev mailing list >>> [email protected] >>> http://wso2.org/cgi-bin/mailman/listinfo/dev >>> >>> >> >> _______________________________________________ >> Dev mailing list >> [email protected] >> http://wso2.org/cgi-bin/mailman/listinfo/dev >> >> > > > Best Regards, > > *Vidura Nanayakkara* > Software Engineer > > Email : [email protected] > Mobile : +94 (0) 717 919277 <+94%2071%20791%209277> > Web : http://wso2.com > > _______________________________________________ > Dev mailing list > [email protected] > http://wso2.org/cgi-bin/mailman/listinfo/dev > > -- Sajith Janaprasad Ariyarathna Senior Software Engineer; WSO2, Inc.; http://wso2.com/ <https://wso2.com/signature>
_______________________________________________ Dev mailing list [email protected] http://wso2.org/cgi-bin/mailman/listinfo/dev
