top of page

Subscribe to Wisdom

Thanks for Subscribing!

Writer's picturePuru Uttam

Loops vs Streams in Java

Updated: May 6, 2022

When we should use loops over streams and vice versa , we should know about the parameters on which it is dependent. Some parameters are mentioned below:

  • Readability : Those who are more familiar with loops may find streams bit hard to read but streams let you decrease the number of lines of code which makes it look more fancy and clean. So you can start learning streams by first writing loops and then try to convert it into streams. It will be more helpful as it will be easy to understand the functionality of each method of stream used.

  • Performance : If the amount of data is less then you can use loops over streams as loops performs better on small list. If the amount of data is huge, then streams should be preferred as parallel streams will perform way better here than loops. Using parallel streams may increase the overhead so it depends on the criticality where saving time is more important than cost. Loops generally win as it can be easily maintained and less overhead than parallel streams. Loop on an array is very lightweight if compared with streams.

  • Debugging : Loops can be debugged more easily than streams without using any extra plugin. Loops win that battle too as streams require extra plugins for debugging so it seams way harder than loops. It doesn't mean you should not try streams as these are more advanced and provide inbuilt functionalities so if you want to learn something new try debugging streams.

  • Streams are more expressive and declarative as it uses Lambdas and functional programming which are the core of Java 8. Loops are traditionally used worldwide and programmers still prefer loops in most of the cases.

Streams provide us various inbuilt methods which gives the advantage over loops as in loop you have to do everything manually. for ex: map(), filter(), collect(), average(), findFirst() or elseNull() and many more. If you are keen to learn, streams can give you such a wide variety where manual coding will be minimized. Streams are the future

Here are some methods below:

  • filter → conditional checks are handled here

  • map → Map or Transform one value to another

  • flatMap → flattens the 2D list in one.

e.g. [ [employee1, employee2], [employee3] ] becomes [employee1, employee2, employee3]

Let's retrieve the list of employees having salary less than 10,000.


public class MyEmployees {
private int id;
private String name;
private int salary;
// constructor, setters, getters  
}

//using Streams
     return employeesList.stream().
     .filter( e -> e.getSalary() < 10000)
     .collect(Collectors.toList());

//using Loop
 List<MyEmployees> employeesHavingLessSalary = new ArrayList<>();
 for(MyEmployees e : employeesList) {
     if(e.getSalary() < 10000) {
         employeesHavingLessSalary.add(e);
     }
 }
 return employeesHavingLessSalary;

21 views0 comments

Recent Posts

See All

Comments


Modern Digital Watch
bottom of page