Showing posts with label IntStream. Show all posts

Using IntStream over for loop in Java 8

Stream API Java 8

We are using for-loop since we know Java. Billions of program running based on this for-loop but time has changed. Its not just creating program that gives us desired output but your program should be faster, utilize the hardware and perform at its pick level.

Java designers did some serious changes in Java8. Lambda expression and Stream API were introduced to in Java8 and that made programmers life much more easy than ever before.

Source code (Simple for-loop)
public class ForLoopExample {

    public static void main(String[] args) {
        /* Create String array */
        String[] arrayNames = {"Vicky Thakor", "Chirag Thakor", "Dave Hill", "Finn Jones"};

        for (int i = 0; i < arrayNames.length; i++) {
            /*Filter name which ends with "Thakor". */
            if (arrayNames[i].endsWith("Thakor")) {
                /* Print name to console. */
                System.out.println(arrayNames[i]);
            }
        }
    }
}

Output
Vicky Thakor
Chirag Thakor

Sour code (IntStream)
import java.util.stream.IntStream;

public class IntStreamExample {

    public static void main(String[] args) {
        /* Loop through Array using IntStream */
        new IntStreamExample().UsingStream();

        /* Loop through Array using IntStream with ParallelStream. */
        new IntStreamExample().UsingParallelStream();
    }

    /**
     * Loop through Array using IntStream
     */
    public void UsingStream() {
        System.out.println("Using IntStream");
        System.out.println("------------------------------------------");

        /* Create String array */
        String[] arrayNames = {"Vicky Thakor", "Chirag Thakor", "Dave Hill", "Finn Jones"};

        /**
         * - Start loop from "range(startInclusive {initial value}, endExclusive {upper limit}}
         * - Filter data.(If condition)
         * - forEach (value that matches filter condition)
         */
        IntStream.range(0, arrayNames.length)
                 .filter(i - > arrayNames[i].endsWith("Thakor"))
                 .forEach(i - > {
                    System.out.println(arrayNames[i]);
                 });
        System.out.println("+++++++++++++++++++++++++++");
    }

    /**
     * Loop through Array using IntStream with ParallelStream.
     * Note: Use it when you want to divide your task in multiple core of CPU and order doesn't matter for the operation.
     */
    public void UsingParallelStream() {
        System.out.println("Using IntStream with ParallelStream");
        System.out.println("------------------------------------------");

        /* Create String array */
        String[] arrayNames = {"Vicky Thakor", "Chirag Thakor", "Dave Hill", "Finn Jones"};

        /**
         * - Start loop from "range(startInclusive {initial value}, endExclusive {upper limit}}
         * - ParallelStream (Perform your task by dividing it in CPU core.) 
         * - Filter data. (If condition)
         * - forEach (value that matches filter condition)
         */
        IntStream.range(0, arrayNames.length)
                 .parallel()
                 .filter(i - > arrayNames[i].endsWith("Thakor"))
                 .forEach(i - >  {
                    System.out.println(arrayNames[i]);
                 });
        System.out.println("+++++++++++++++++++++++++++");
    }
}

Output
Using IntStream
------------------------------------------
Vicky Thakor
Chirag Thakor
+++++++++++++++++++++++++++
Using IntStream with ParallelStream
------------------------------------------
Chirag Thakor
Vicky Thakor
+++++++++++++++++++++++++++

Note:
Use ParallelStream only when you want to divide your task in multiple core of CPU and order doesn't matter for your operation.