foreach() loop vs Stream foreach() vs Parallel Stream foreach() Last Updated : 12 Jul, 2025 Comments Improve Suggest changes 11 Likes Like Report foreach() loopLambda operator is not used: foreach loop in Java doesn't use any lambda operations and thus operations can be applied on any value outside of the list that we are using for iteration in the foreach loop. The foreach loop is concerned over iterating the collection or array by storing each element of the list on a local variable and doing any functionality that we want. Java public class GFG { public static void main(String[] args) { String[] arr = { "1", "2", "3" }; int count = 0; String[] arr1 = { "Geeks", "For", "Geeks" }; for (String str : arr) { System.out.print(arr1[count++]); } } } Output: GeeksForGeeksHere operations on count are possible even being a variable outside the loop because it is in the scope of the foreach loop. It can be used for all collections and arrays: It can be used to iterate all collections and arrays in Java. Java public class GFG { public static void main(String[] args) throws Exception { String[] arr = { "1", "2", "3" }; int count = 0; String[] arr1 = { "Geeks", "For", "Geeks" }; for (String str : arr) { System.out.print(arr1[count++]); } } } Output: GeeksForGeeksThe return statements work within the loop: The function can return the value at any point of time within the loop. For example, if we want to print only the first 2 values of any collection or array and then we want to return any value, it can be done in foreach loop in Java. The code below is for printing the 2nd element of an array. Java public class GFG { public static String frechlop(String[] geek) { int count = 0; for (String var : geek) { if (count == 1) return var; count++; } return ""; } public static void main(String[] args) throws Exception { String[] arr1 = { "Geeks", "For", "Geeks" }; String secelt = frechlop(arr1); System.out.println(secelt); } } Output: Forstream().forEach()Lambda operator is used: In stream().forEach(), lambdas are used and thus operations on variables outside the loop are not allowed. Only the operations on concerned collections are possible. In this, we can perform operations on Collections by single line code that makes it easy and simple to code. Java import Java.util.*; public class GFG { public static void main(String[] args) throws Exception { List<String> arr1 = new ArrayList<String>(); int count = 0; arr1.add("Geeks"); arr1.add("For"); arr1.add("Geeks"); arr1.stream().forEach(s -> { // this will cause an error count++; // print all elements System.out.print(s); }); } } Note: The operation "count++" will cause an error because lambda operations do not allow any external variable operation within itself. Only used to iterate collections: The stream().forEach() is only used for accessing the collections like set and list. It can also be used for accessing arrays. Java import java.util.*; public class GFG { public static void main(String[] args) throws Exception { String[] arr1 = { "Geeks", "for", "Geeks" }; // The next line will throw error // as there is no such method as stream() arr1 .stream() .forEach(s -> { System.out.print(s); }); } } Return statements don't work within this loop but the function calls are very easy to call: The return statement within the loop doesn't work. The same functionality of getting 2nd element cannot be done in the same forEach() method. Java import Java.util.*; public class GFG { static String second(List<String> list) { list .stream() .forEach( s -> { // The next line will throw error // as no return allowed // if(s=="For")return s; }); return "null"; } public static void main(String[] args) throws Exception { List<String> arr1 = new ArrayList<String>(); arr1.add("Geeks"); arr1.add("For"); arr1.add("Geeks"); String f = second(arr1); System.out.println(f); } } Output: nullparallel foreach()Works on multithreading concept: The only difference between stream().forEach() and parallel foreach() is the multithreading feature given in the parallel forEach().This is way more faster that foreach() and stream.forEach(). Like stream().forEach() it also uses lambda symbol to perform functions. The example providing its multithreading nature which is given as follows.It's clearly implied that the output will never be in the same order for printing the strings due to parallelStream's multithreaded nature. Java import Java.util.*; public class GFG { public static void main(String[] args) throws Exception { List<String> arr1 = new ArrayList<String>(); arr1.add("Geeks"); arr1.add("For"); arr1.add("Geeks"); arr1 .parallelStream() .forEach( s -> { System.out.print(s); }); } } Output: ForGeeksGeeks Tabular differenceforeach() loopstream().foreach() loopparallelStream().foreach() loopLambda operators is not usedLambda operator is usedLambda operator is usedCan be used to access arrays and collectionsCan access collections onlyCan access collections onlyThe return or control statements work within the loopThe return or control statements don't work within the loopThe return or control statements don't work within the loopNo multithreading thus slow data is in sequenceNo multithreading thus slow data is in sequenceIt is multithreaded thus very fast and sequence is different Create Quiz Comment P piyush25pv Follow 11 Improve P piyush25pv Follow 11 Improve Article Tags : Java Difference Between Explore Java BasicsIntroduction to Java3 min readJava Programming Basics9 min readJava Methods6 min readAccess Modifiers in Java4 min readArrays in Java7 min readJava Strings7 min readRegular Expressions in Java3 min readOOP & InterfacesClasses and Objects in Java5 min readAccess Modifiers in Java4 min readJava Constructors4 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages2 min readJava Interface7 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java4 min readIterator in Java4 min readJava Comparator Interface5 min readException HandlingJava Exception Handling6 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java7 min readFile Handling in Java4 min readJava Method References7 min readJava 8 Stream Tutorial7 min readJava Networking6 min readJDBC Tutorial5 min readJava Memory Management3 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers1 min readJava Programs - Java Programming Examples7 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz1 min readJava Project Ideas For Beginners and Advanced15+ min read Like