Open In App

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

foreach() loop




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: 
GeeksForGeeks




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: 

GeeksForGeeks




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: 
For

stream().forEach()




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);
        });
    }
}




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);
            });
    }
}




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: 

null

parallel foreach()




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 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

Article Tags :