I've started learning Java-8 streams lately. I'm looking at a huge code base and trying to figure out where can I use streams to improve performance.
From what I've understood, replacing every loop with streams doesn't make sense as streams are not always better than loops. Following are few cases I've understood to be beneficial:
while(iterate over a collection) { if(certain condition is met) { //do some computation } else { //do nothing } }
Using streams here avoids loading the needless members of the collection here. (faster & memory efficient)
Please correct me if I'm wrong. I'm looking for more such cases...
Going back over a codebase and rewriting things by hand is probably a waste of time. Generally speaking, streams aren't going to provide a performance benefit unless you're using parallel streams over a very large computation (see http://gee.cs.oswego.edu/dl/html/StreamParallelGuidance.html for detailed advice). Most of the time, streams will have ~equal performance; they'll just be shorter to write.
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