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

  • International Labour's Day in Hindi | अंतर्राष्ट्रीय मजदूर दिवस 1 मई
    International Labour’s Day in Hindi | अंतर्राष्ट्रीय मजदूर दिवस 1 मई General Knowledge
  • Kishkindha Kand in Hindi | Ram meets Hanuman | किष्किंधा कांड
    Kishkindha Kand in Hindi | Ram meets Hanuman | किष्किंधा कांड Spiritual
  • What is Pronoun?
    What is Pronoun with example? Pronoun definition and examples Grammar
  • Subhas Chandra Bose Biography in Hindi, Essay, Paragraph
    Subhas Chandra Bose Biography in Hindi, Essay, Paragraph Biography
  • Kishore Kumar Biography in Hindi | किशोर कुमार की जीवनी
    Kishore Kumar Biography in Hindi | किशोर कुमार की जीवनी Biography
  • Vat Savitri Vrat in Hindi, Vat Savitri Puja | वट सावित्री पूजा
    Vat Savitri Vrat in Hindi, Vat Savitri Puja | वट सावित्री पूजा Festival
  • World Earth Day in Hindi | पृथ्वी दिवस कब और क्यों मनाया जाता है?
    Earth Day in Hindi, Theme | पृथ्वी दिवस कब और क्यों मनाया जाता है? General Knowledge
  • Amitabh Bachchan biography in hindi | Family, wife, awards
    Amitabh Bachchan biography in hindi | Family, wife, awards Biography

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 Learning Tutorial Java Tutorial for Advanced Learning, Buzzwords, Parts of Java Java
Jenkins java | Installing Jenkins on Windows Jenkins java | Installing Jenkins on Windows Important
Difference between Git Merge and Git Rebase | Git Rebase vs Git Merge Difference between Git Merge and Git Rebase | Git Rebase vs Git Merge Differences
Immutable class in Java Immutable class in Java | How to create Immutable class in Java Important
Difference between Comparable and Comparator Difference between Comparable and Comparator Differences
Variables in Java Variables in Java Java

Leave a Reply Cancel reply

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

  • National Farmers Day in Hindi | राष्ट्रीय किसान दिवस पर निबंध | चौधरी चरण सिंह जयंती
  • Human rights day in Hindi: 10 दिसंबर ह्यूमन राइट्स डे
  • Unicef day is celebrated on December 11 | Speech on unicef day
  • Indian Navy Day: जल सेना दिवस कब और क्यों मनाया जाता है?
  • P V Sindhu Biography in Hindi, Badminton, State, Caste पी. वी. सिंधु जीवन परिचय, कहानी, राज्य, जाति
  • Draupadi Murmu Biography In Hindi | द्रौपदी मुर्मू की जीवनी
  • Similarities and difference between OSI and TCP/IP model
    OSI vs TCP/IP Model, Similarities and difference between OSI and TCP/IP model Networking
  • Network kya hai (नेटवर्क क्या है)
    Network kya hai (नेटवर्क क्या है) Networking
  • TCP/IP Model, Full Form, Layers and their Functions
    TCP/IP Model, Full Form, Layers and their Functions Networking
  • OSI Model | 7 Layers of OSI Model in Computer network
    OSI Model | 7 Layers of OSI Model in Computer network, Functions Networking
  • IPv4 Vs IPv6 | Difference between IPv4 and IPv6
    IPv4 Vs IPv6 | Difference between IPv4 and IPv6 Differences
  • Difference between TCP and UDP
    Difference between TCP and UDP | TCP vs UDP examples Differences
  • Difference between Internet and Intranet
    Difference between Internet and Intranet Differences
  • 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