parallel streams in Java 8 | Streams in Java 8 | Onlinetutorialspoint

PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
JAVAEXCEPTIONSCOLLECTIONSSWINGJDBC
EXCEPTIONS
COLLECTIONS
SWING
JDBC
JAVA 8
SPRING
SPRING BOOT
HIBERNATE
PYTHON
PHP
JQUERY
PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
Parallel Streams are greatest addition to Java 8 after Lambdas. The actual essence of Stream API can only be observed if used as parallel.
Suppose let’s take a scenario of you having a list of employee objects and you have to count employees whose salary is above 15000. Generally, to solve this problem you will iterate over list going through each and every employee and checking if employees salary is above 15000. This takes O(N) time since you go sequentially.
Streams provide us with the flexibility to iterate over the list in a parallel pattern and can give the aggregate in quick fashion.
Stream implementation in Java is by default sequential unless until it is explicitly mentioned for parallel. When a stream executes in parallel, the Java runtime partitions the stream into multiple substreams. Aggregate operations iterate over and process these sub-streams in parallel and then combine the results.
The only thing to keep in mind to create parallel stream is to call parallelStream() on the collection else by default sequential stream gets returned by stream().
We have created a list of 600 employees out of which there are 300 employees whose salary is above 15000.
Creating a sequential stream and filtering elements it took above 40 milliseconds, whereas the parallel stream only took 4 milliseconds.
import java.util.ArrayList;
import java.util.List;
public class ParallelStream {
 public static void main(String[] args) {
  List < Employee > empList = new ArrayList < Employee > ();
  for (int i = 0; i < 100; i++) {
   empList.add(new Employee("A", 20000));
   empList.add(new Employee("B", 3000));
   empList.add(new Employee("C", 15002));
   empList.add(new Employee("D", 7856));
   empList.add(new Employee("E", 200));
   empList.add(new Employee("F", 50000));
  }
  long t1 = System.currentTimeMillis();
  System.out.println("Sequential Stream count: " + empList.stream().filter(e -> e.getSalary() > 15000).count());
  long t2 = System.currentTimeMillis();
  System.out.println("Sequential Stream Time taken:" + (t2 - t1));
  t1 = System.currentTimeMillis();
  System.out.println("Parallel Stream count: " + empList.parallelStream().filter(e -> e.getSalary() > 15000).count());
  t2 = System.currentTimeMillis();
  System.out.println("Parallel Stream Time taken:" + (t2 - t1));
 }
}
Employee.java

class Employee {

    private int salary;
    private String name;

    Employee(String name, int salary) {
        this.name = name;
        this.salary = salary;
    }

    public int getSalary() {
        return salary;
    }

    public void setSalary(int salary) {
        this.salary = salary;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

Sequential Stream count: 300
Sequential Stream Time taken:59
Parallel Stream count: 300
Parallel Stream Time taken:4
Performance Implications:
Parallel Stream has equal performance impacts as like its advantages. Since each substream is a single thread running and acting on the data, it has overhead compared to sequential stream. Inter-thread communication is dangerous and takes time for coordination
When to use Parallel Streams:
They should be used when the output of the operation is not needed to be dependent on the order of elements present in source collection (on which stream is created).
Parallel Streams can be used in case of aggregate functions.
Iterate over large sized collections.
If you have performance implications with sequential streams.
If your environment is not multi threaded, because parallel stream creates thread and can effect new requests coming in.
Happy Learning 🙂

How to Merge Streams in Java 8
How to Sort an Array in Parallel in Java 8
How to calculate Employees Salaries Java 8 summingInt
How to Convert Iterable to Stream Java 8
What is Association in Java
Java 8 Stream API and Parallelism
Java 8 Stream Filter Example with Objects
Java 8 groupingBy Example
How to get Stream count in Java 8
What are Lambda Expressions in Java 8
How Java 8 Stream generate random String and Numbers
Java – How to Convert InputStream to String
Java 8 foreach Example Tutorials
How to convert List to Map in Java 8
Java 8 How to Convert List to String comma separated values

How to Merge Streams in Java 8
How to Sort an Array in Parallel in Java 8
How to calculate Employees Salaries Java 8 summingInt
How to Convert Iterable to Stream Java 8
What is Association in Java
Java 8 Stream API and Parallelism
Java 8 Stream Filter Example with Objects
Java 8 groupingBy Example
How to get Stream count in Java 8
What are Lambda Expressions in Java 8
How Java 8 Stream generate random String and Numbers
Java – How to Convert InputStream to String
Java 8 foreach Example Tutorials
How to convert List to Map in Java 8
Java 8 How to Convert List to String comma separated values






priya
July 11, 2018 at 12:33 pm - Reply 

A stream is a sequence of data. The InputStream is used to read data from a source and the OutputStream is used for writing data to a destination. InputStream and OutputStream are the basic stream classes in Java.










priya
July 11, 2018 at 12:33 pm - Reply 

A stream is a sequence of data. The InputStream is used to read data from a source and the OutputStream is used for writing data to a destination. InputStream and OutputStream are the basic stream classes in Java.




A stream is a sequence of data. The InputStream is used to read data from a source and the OutputStream is used for writing data to a destination. InputStream and OutputStream are the basic stream classes in Java.
 


Δ
 Install Java on Mac OS
 Install AWS CLI on Windows
 Install Minikube on Windows
 Install Docker Toolbox on Windows
 Install SOAPUI on Windows
 Install Gradle on Windows
 Install RabbitMQ on Windows
 Install PuTTY on windows
 Install Mysql on Windows
 Install Hibernate Tools in Eclipse
 Install Elasticsearch on Windows
 Install Maven on Windows
 Install Maven on Ubuntu
 Install Maven on Windows Command
 Add OJDBC jar to Maven Repository
 Install Ant on Windows
 Install RabbitMQ on Windows
 Install Apache Kafka on Ubuntu
 Install Apache Kafka on Windows
 Java8 – Install Windows
 Java8 – foreach
 Java8 – forEach with index
 Java8 – Stream Filter Objects
 Java8 – Comparator Userdefined
 Java8 – GroupingBy
 Java8 – SummingInt
 Java8 – walk ReadFiles
 Java8 – JAVA_HOME on Windows
 Howto – Install Java on Mac OS
 Howto – Convert Iterable to Stream
 Howto – Get common elements from two Lists
 Howto – Convert List to String
 Howto – Concatenate Arrays using Stream
 Howto – Remove duplicates from List
 Howto – Filter null values from Stream
 Howto – Convert List to Map
 Howto – Convert Stream to List
 Howto – Sort a Map
 Howto – Filter a Map
 Howto – Get Current UTC Time
 Howto – Verify an Array contains a specific value
 Howto – Convert ArrayList to Array
 Howto – Read File Line By Line
 Howto – Convert Date to LocalDate
 Howto – Merge Streams
 Howto – Resolve NullPointerException in toMap
 Howto -Get Stream count
 Howto – Get Min and Max values in a Stream
 Howto – Convert InputStream to String