parallel streams in java

Posted By : Mohit Gupta | 30-Jun-2021

Java Parallel Stream

* Java parallel streams are a feature of java 8, it means utilizing multiple cores of the processor.

*Normally any java code has one stream of processing, where it is executed sequentially .where as by using parallel streams, we can divide the code into multiple streams that are executed in parallel on separate cores and the final result is the combination of the individual outcomes

*However, the order of execution is not under our control.


sequential vs parallel stream execution

sequential->The task will use multiple threads but in work on  a single core

parallel steam execution-> The task will use multiple threads but in work on multiple cores 

Example of parallel stream

public class ParallelStreamExample {

    public static void main(String[] args) {
        long start=0;
        long end=0;

        System.out.println("Plain stream took time : "+(end-start));


        System.out.println("Parallel stream took time : "+(end-start));

            System.out.println("Thread : "+Thread.currentThread().getName()+" : "+x);

            System.out.println("Thread : "+Thread.currentThread().getName()+" : "+x);

        List<Employee> employees = EmployeeDatabase.getEmployees();

        double salaryWithStream =
                .map(Employee::getSalary).mapToDouble(i -> i).average().getAsDouble();

        System.out.println("Normal stream execution time : "+(end-start)+" : Avg salary : "+salaryWithStream);

        double salaryWithParallelStream = employees.parallelStream()
                .map(Employee::getSalary).mapToDouble(i -> i).average().getAsDouble();


        System.out.println("Parallel stream execution time : "+(end-start)+" : Avg salary : "+salaryWithParallelStream);



Java 8 has introduced a new class Optional in java.util package. Without using too many null checks, it can help in writing a neat code. By using Optional.

some examples of optional interface ->

1 step ->create a class customer

public class Customer {

    private int id;
    private String name;
    private String email;
    private List<String> phoneNumbers;

    public Customer() {

    public Customer(int id, String name, String email, List<String> phoneNumbers) { = id; = name; = email;
        this.phoneNumbers = phoneNumbers;

    public int getId() {
        return id;

    public void setId(int id) { = id;

    public String getName() {
        return name;

    public void setName(String name) { = name;

    public Optional<String> getEmail() {
        return Optional.ofNullable(email);

    public void setEmail(String email) { = email;

    public List<String> getPhoneNumbers() {
        return phoneNumbers;

    public void setPhoneNumbers(List<String> phoneNumbers) {
        this.phoneNumbers = phoneNumbers;

2 . step ->create a database Ekart Database


public class EkartDataBase {

    public static List<Customer> getAll() {
        return Stream.of(
                new Customer(101, "john", "", Arrays.asList("397937955", "21654725")),
                new Customer(102, "smith", "", Arrays.asList("89563865", "2487238947")),
                new Customer(103, "peter", "", Arrays.asList("38946328654", "3286487236")),
                new Customer(104, "kely", "", Arrays.asList("389246829364", "948609467"))

3 step ->create an OptionalDemo class 


public class OptionalDemo {

    public static Customer getCustomerByEmailId(String email) throws Exception {
        List<Customer> customers = EkartDataBase.getAll();
                .filter(customer -> customer.getEmail().equals(email))
                .findAny().orElseThrow(()->new Exception("no customer present with this email id"));


    public static void main(String[] args) throws Exception {

        Customer customer=new Customer(101, "john", "", Arrays.asList("397937955", "21654725"));


        Optional<Object> emptyOptional = Optional.empty();

        //Optional<String> emailOptional = Optional.of(customer.getEmail());

        Optional<String> emailOptional2 = Optional.ofNullable(customer.getEmail());
       /* if(emailOptional2.isPresent()){
       // System.out.println(emailOptional2.orElse(""));

       // System.out.println(emailOptional2.orElseThrow(()->new IllegalArgumentException("email not present")));

        System.out.println(>"default email..."));




About Author

Author Image
Mohit Gupta

Mohit Gupta is working as a Developer, having good knowledge of core java. he is good in behaviour and and prefers to complete assigned tasks on time. 

Request for Proposal

Name is required

Comment is required

Sending message..