Skip to content
  • Facebook
GeekCer Logo

GeekCer

The geek's Coding education and Review centre

  • Home
  • Tutorials
    • Java
    • Servlet
    • JSP
    • Python
    • C Tutorial
    • Spring
    • Spring Boot
    • MongoDB
    • Hibernate
    • Data Structure
  • General Knowledge
  • Biography
  • Grammar
  • Festival (त्योहार)
  • Interview
  • Differences
  • Important
  • Toggle search form

Home » Java » Sequential and Parallel Stream in Java | Example, Differences

  • Top HR Interview Questions and Answers for Job Seekers
    Top HR Interview Questions and Answers for Job Seekers Interview
  • Stack Vs Heap in Java
    Stack Vs Heap in Java | Memory Allocation Section in Java Differences
  • Jenkins java | Installing Jenkins on Windows
    Jenkins java | Installing Jenkins on Windows Important
  • Kapil Sharma Show, Comedy Show in Hindi
    Kapil Sharma Show, Comedy Show in Hindi Biography
  • Bhagat Singh Biography in Hindi (भगत सिंह का जीवन परिचय)
    Bhagat Singh Biography in Hindi (भगत सिंह का जीवन परिचय) Biography
  • Famous Slogans given by famous Personalities Indian leaders
    Famous Slogans given by famous Personalities Indian leaders General Knowledge
  • Data Independence in DBMS and Types (Logical and Physical)
    Data Independence in DBMS and Types (Logical and Physical) dbms
  • String Array in C | What is an array of string | String Functions
    String Array in C | What is an array of string | String Functions C Language

Sequential and Parallel Stream in Java | Example, Differences

Posted on June 11, 2022June 11, 2022 By GeekCer Education No Comments on Sequential and Parallel Stream in Java | Example, Differences
Sequential and Parallel Stream in Java | Example, Differences

Let’s look at the differences between Sequential and parallel stream in Java 8, as well as when to use parallel streams in Java 8. Sequential stream is based on a single core and works similar as for loop. Parallel stream makes use of the computer’s numerous cores and divides the job into multiple subtasks, each of which is executed in its own thread.

By default, any stream action in Java is performed sequentially. Sequential stream never makes use of the computer’s multi-core. This is the major drawback of sequential stream.

When it comes to Parallel Streams, Java provides some methods for achieving parallelism. The parallel stream is prone to errors and is quite complex. However, Java Streams provides a reliable library that takes care of all the complexity, and we just need to use a few methods to achieve parallelism.

It is true that Java 8 supports both parallel and sequential stream. We can use both types of streams according to our need.

Table of Contents

  • Sequential Stream in Java example
  • Parallel Stream in Java example
  • Sequential Stream Vs Parallel Stream
  • When to use parallel streams in java 8?
  • Disadvantages of parallel stream in java 8

Sequential Stream in Java example

By default the stream is sequential. If a stream operation is not marked as parallel, it is considered as a sequential stream. When we employ a sequential stream, we are not taking use of a multi-core system, even though the underlying system allows for parallel processing.

The stream() method in Java 8 returns a sequential stream.


import java.util.Arrays;
import java.util.List;

public class SequentialStreamExample {
	public static void main(String[] args) {
		List inputList = Arrays.asList("Welcome ", "to ", "Geekcer ", "Education");

		inputList.stream().forEach(System.out::print);
	}
}

Output:
Welcome to Geekcer Education

Parallel Stream in Java example

When we use the parallel stream functionality in our code, it is sliced into many streams that run in parallel on the system’s different cores.

Due to the use of multiple cores, parallel streams improve performance. It is not necessary that all programs/code be parallelized. However, in parts of the code where there is a lot of processing and we need to increase speed, we can use parallel stream.

There are mainly two options for accomplishing parallel functionality.

  • By using the collection’s parallelStream() method to create a parallel stream.
  • By applying BaseStream’s parallel() method to a sequential stream.

import java.util.Arrays;
import java.util.List;

public class ParallelStreamExample {
	public static void main(String[] args) {
		List inputList = Arrays.asList("Welcome ", "to ", "Geekcer ", "Education ");

		inputList.parallelStream().forEach(System.out::print);
	}
}

Output:
Geekcer Education to Welcome 

Example using the forEachOrdered() method to get the data in order.


import java.util.Arrays;
import java.util.List;

public class ParallelStreamExample {
	public static void main(String[] args) {
		List inputList = Arrays.asList("Welcome ", "to ", "Geekcer ", "Education ");

		inputList.parallelStream().forEachOrdered(System.out::print);
	}
}

Output:
Welcome to Geekcer Education 

Sequential Stream Vs Parallel Stream

Sequential StreamParallel Stream
Utilize single core of the system.Utilize multi-core of the system.
Cares about the order.Doesn’t care about the order.
Sequential Stream is platform Independent.Parallel Stream is platform dependent.
Sequential Stream is more reliable and more error-free.In a sequential stream, one iteration waits for the current iteration to finish before proceeding.
In a sequential stream, one iteration waits for the current iteration to finish before proceeding.In a parallel stream, an iteration does not wait for the current iteration to finish in order to proceed.

When to use parallel streams in java 8?

  • You have a large dataset in which you want to perform operations.
  • If sequential streams have a performance impact, parallel streams can be used.
  • If you want to use all the cores of your system.

Disadvantages of parallel stream in java 8

Parallel stream divides them into sub-problems, which are then processed on separate threads, which can be shifted to other cores and then merged after they’re finished. If you don’t use it correctly, it might actually slow down your code.

Unexpected effects may occur if all shared resources between threads are not properly synchronized.

Recommended Articles

  • What is Lambda expression with Example
  • map() function of Stream in Java 8
  • Runnable Vs Callable in Java
  • Fork/Join for the Processing in Java 8
  • Java Concurrent Collections with example

Share this:

  • Click to share on Facebook (Opens in new window)
  • Click to share on WhatsApp (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • More
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Pinterest (Opens in new window)

Also Read

Differences, Java Tags:Java Parallel Stream : When to Use it?, Parallel and serial streams, Parallel data processing and performance

Post navigation

Previous Post: Java 8 Stream map example | Java 8 map(function) parameter
Next Post: Internal Working of HashMap in Java, Rehashing, Collision

More Related Articles

Java Vector Java Vector Java
Difference between JPanel and JFrame Difference between JPanel and JFrame Differences
Jenkins java | Installing Jenkins on Windows Jenkins java | Installing Jenkins on Windows Important
Fork/Join Framework in Java | RecursiveTask, RecursiveAction Fork/Join Framework in Java | RecursiveTask, RecursiveAction Java
What is Array in Java? Types of array in Java What is Array in Java? Types of array in Java Java
Difference between SQL and HQL Difference between SQL and HQL | Hql vs Sql performance Differences

Related Posts

  • Java Vector
    Java Vector Java
  • Difference between JPanel and JFrame
    Difference between JPanel and JFrame Differences
  • Jenkins java | Installing Jenkins on Windows
    Jenkins java | Installing Jenkins on Windows Important
  • Fork/Join Framework in Java | RecursiveTask, RecursiveAction
    Fork/Join Framework in Java | RecursiveTask, RecursiveAction Java
  • What is Array in Java? Types of array in Java
    What is Array in Java? Types of array in Java Java
  • Difference between SQL and HQL
    Difference between SQL and HQL | Hql vs Sql performance Differences

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • National Doctors Day in India and Other Countries, July 1, 2022
  • America Independence Day : 4th July USA | USA Birthday
  • Ganesh Chaturthi Puja in Hindi | गणेश चतुर्थी का व्रत, महत्व, कथा
  • जन्माष्टमी व्रत पूजा विस्तार से | दही हांडी | Krishna Janmashtami Puja
  • Jagannath Rath Yatra History in Hindi | जगन्नाथ पुरी की कहानी
  • Prithviraj Chauhan Biography in Hindi | पृथ्वीराज चौहान जीवनी हिंदी
  • Internal Working of HashMap in Java, Rehashing, Collision
  • MS Dhoni (Mahendra singh Dhoni) Cricket Biography in Hindi
  • Top HR Interview Questions and Answers for Job Seekers
    Top HR Interview Questions and Answers for Job Seekers Interview
  • Stack Vs Heap in Java
    Stack Vs Heap in Java | Memory Allocation Section in Java Differences
  • Jenkins java | Installing Jenkins on Windows
    Jenkins java | Installing Jenkins on Windows Important
  • Kapil Sharma Show, Comedy Show in Hindi
    Kapil Sharma Show, Comedy Show in Hindi Biography
  • Bhagat Singh Biography in Hindi (भगत सिंह का जीवन परिचय)
    Bhagat Singh Biography in Hindi (भगत सिंह का जीवन परिचय) Biography
  • Famous Slogans given by famous Personalities Indian leaders
    Famous Slogans given by famous Personalities Indian leaders General Knowledge
  • Data Independence in DBMS and Types (Logical and Physical)
    Data Independence in DBMS and Types (Logical and Physical) dbms
  • String Array in C | What is an array of string | String Functions
    String Array in C | What is an array of string | String Functions C Language
  • Java Tutorial
  • Servlet Tutorial
  • JSP Tutorial
  • Maven Tutorial
  • HTML Tutorial
  • Programs
  • Hindi/English Grammar
  • Difference Between ... and ...
  • HR Interview
  • Important Articles

Write to Us:
geekcer.code@gmail.com

  • About Us
  • Privacy and Policy
  • Disclaimer
  • Contact Us
  • Sitemap

Copyright © GeekCer 2022 All Rights reserved