Is stream better than an iterative approach (e.g. For loop)?

Source: https://www.incimages.com/uploaded_files/image/1920x1080/getty_506903004_200013332000928076_348061.jpg

In today’s data rich and integrated world, we are often dealing with streams especially if you are a java programmer. However, how many of us(java programmers) know the internal workings of streams or have a point of view on whether- is stream better than a simple For loop ?

If you know these things, then I believe this blog may not be relevant for you. If not, then let’s dig deeper.

Since I asked you this question so let us start with the internal working of ‘For each’ loop.
for(int x:list)
This is a sugar-coated line that wrapped up the internal iteration code.

Iterator<Integer> iterator = list.iterator();
while(iterator.hasNext()){
Integer x=iterator.next();
}

This is what is happening when we are calling a For loop. The logic behind this is that the iterator is created over your application code and for every iteration, it will go to collection and collect whatever it requires and comes back to the application code.
It is not possible to process elements of the collection in a parallel way if we are using a For loop. With a For loop, it is not easy to implement something which will do more than one operation on it. E.g., if you want to change the element of a list to upper case, filter something out of it, and then store it in a list, then it’s challenging.

List<String> updatedList=new ArrayList<>();
for(String value:list){
String valueInUpperCase = value.toUpperCase();
if(isMatching(valueInUpperCase)){
updatedList.add(valueInUpperCase);
}
}

All these difficulties are there because iteration is done on code, not on collection. Here stream comes with simple logic that iteration will be done over-collection internally, not on application code. In technical terms, this is internal iteration and the one with for loop is an external iteration on application code. This logic will mitigate both issues mentioned above. since the stream is doing iteration internally on the collection, it can quickly be processed parallelly as there is no dependency of one element over another and with streams, we can do more than one operation quite easily.

For the same example, which was difficult by a single For loop, we can call streams and convert it to uppercase and filter it as required.

list.stream().map(x ->x.toUpperCase()).filter(ismatching()).collect(Collectors.toList());

Its much more readable as compared to its For loop implementation.

With all these points, you might think that we should remove For loop from java and use streams everywhere. However, that is not the case. Streams have their disadvantages as well.

Performance ->of course, performance-wise, a simple For loop is better than a stream as streams have quite a bit of overhead.
Readability -> we are familiar with a simple For loop for long, and streams are not that old when we compare it with For loop. That’s one of the reasons we believe simple For loop is more readable, and to some extent, it is correct. For example, when we are using 2 For loops, one inside another, it is readable. Nevertheless, when we have to do the same thing in streams, you will feel it’s complex. So I believe it all comes down to how much familiar we are with streams.
Debugging -> simple For loops is more accessible to debug than streams as legacy debuggers are not equipped with the streams.

As a conclusion, I feel that both streams or For loop are good in their own ways and we developers need to think about which one to use according to our requirements. We need to be equipped with the associated trade-offs and decide when to use streams and when to go for a For loop.

Is stream better than an iterative approach (e.g. For loop)? was originally published in Walmart Global Tech Blog on Medium, where people are continuing the conversation by highlighting and responding to this story.

Article Link: Is stream better than an iterative approach (e.g. For loop)? | by Divyanshu | Walmart Global Tech Blog | Medium