A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/java/foreach-loop-vs-stream-foreach-vs-parallel-stream-foreach/ below:

foreach() loop vs Stream foreach() vs Parallel Stream foreach()

foreach() loop vs Stream foreach() vs Parallel Stream foreach()

Last Updated : 12 Jul, 2025

foreach() loop 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++]);
        }
    }
}
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++]);
        }
    }
}
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);
    }
}
stream().forEach() 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);
        });
    }
}
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);
            });
    }
}
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);
    }
}
parallel foreach() 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);
                });
    }
}
Tabular difference foreach() loop stream().foreach() loop parallelStream().foreach() loop Lambda operators is not used Lambda operator is used Lambda operator is used Can be used to access arrays and collections Can access collections only Can access collections only The return or control statements work within the loop The return or control statements don't work within the loop The return or control statements don't work within the loop No multithreading thus slow data is in sequence No multithreading thus slow data is in sequence It is multithreaded thus very fast and sequence is different

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4