how to implement Parallel streams in Java 8:

    When you want to process the data concurrently across multiple process, what will you do???

Java 8 offers you a concept…

That is “Parallel Streams”.

The common usage of parallel Streams is listed below.

Batch Processing: Reading sensor inputs and log files can be done. These can be concurrently   

                                processed across multiple places.

Data Processing: It deals with large amount of data. It collects large amount of data, process it by

                               streams.

Data Analysis:  This process Analyse the data collected.

Image and video processing: Image and video is processed according to the standard format. It deals

                                                     with image filteration, video layout editing and so on.

Sorting, Searching and aggregation: These are operations on data. It deals with arranging the data in

                                                               specified format for sorting. Searching is a process of finding                                                                     an element. Aggregation deals with combining data together.

Machine learning: This deals with large sets of data involving machine learning algorithms.

Real time data processing: Real time data is processed concurrently.

How to create a Parallel Streams:

              As we know earlier, parallel streams are executed the process parallelly. So, it uses stream to execute this.

  • Here, there are three built in packages are used.
  • Arrays, List and Stream collectors. There are the sub packages from java.util package.
  • Create a class “StreamEg2” and save this file as “StreamEg2.java”.
  • Inside the main function, create a list as Array
  • Next, call Parallel Stream function and using ForEach method, print the value.

#Java Program to create Parallel streams

import java.util.Arrays;

import java.util.List;

import java.util.stream.Collectors;

   public class StreamEg2 {

       public static void main(String[] args) {

             

List<Integer> list = Arrays.asList(1, 2, 3, 4);

list.parallelStream().forEach(System.out::println);

}

}

Executing this, you will get the below output.

These are the ways of using ParallelStreams. These are useful in realtime applications and large data manipulation applications.

No comments:

Post a Comment