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.