Lambda & Streams

Java Best Practices Series

Lambda & Streams

42. Prefer Lambdas To Anonymous Classes

When using event listeners, comparators, or simple interfaces, which promises one method implementation, it is valuable to use lambdas, since there is no need to use multiple lines of boilerplate code to call the only method available.

Anonymous Class
new Comparator() {
    public int compare(String s1, String s2) {
        return, s2.length());
(s1, s2) ->, s2.length());

43. Use Method References Instead Of Lambdas

Using a lambda can in many cases make the code simpler and easier to read. (see item 42.) However, whenever calling a single method with the arguments matching those of the lambda, it can be replaced with a method reference.


44.  Use Standard Functional Interfaces Whenever Possible

Within java.util.Function, 43 standard functional interfaces have been added. These interfaces can be used as method references, and since they are already included in the standard java library, it would be redundent to recreate you own version of them.




45. Use Streams To Make Readable Code

Streams are a perfect way of making code readable, instead of making multiple lines of nested if-statements and mapping lists of objects to other types of list or single objects.

However, streams can also be overused. This often happens when streams are stretched over several lines. A good way to avoid this, is by using descriptive local methods and storing streams value in similarly descriptive local variables.

46. Prefer Stream Functionalitites Over ForEach

The forEach method is commonly used by developers to perform all sorts of functions on a list of objects.

However, with the introduction of streams, it is preferable to utilize any of the streams many functionalities over the ForEach. (e.g. map, collect, sort, groupingBy etc.)

47. Store Results Of Streams As List, And Not As Streams

While streams are a convenient way of searching and modifying lists of objects, it is not an appropriate way to send collections of objects around the system as a variable.

Stream are a way of handling collections, sets and lists of objects, similar to other iterative methods. For this reason, it is recommended to perform the intended action on the object collection using stream, and then afterwards assemble a new list or single object to send back into the system.

48. Be Cautious When Using The Streams Parallel

The parallel-method of streams, can potentially assist in create concurrent program flows. However, it should be considered a doubled edged sword, since it also have the capabilities to lock or even crash the program.

This can for example happen when used in combination stream.iterative or the limit method.

Recommended Reading



Hi, I'm the Author

My name is Daniel H. Jacobsen and I’m a dedicated and highly motivated software developer with a masters engineering degree within the field of ICT. 

I have through many years of constantly learning and adapting to new challenges, gained a well-rounded understanding of what it takes to stay up to date with new technologies, tools and utilities. 

The purpose of this blog is to share both my learnings and knowledge with other likeminded developers as well as illustrating how these topics can be taught in a different and alternative manner.

If you like the idea of that, I would encourage you to sign up for the newsletter.

Cheers! 🍺

Didn't Find What You Were Looking For?

Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages
Scroll to Top