EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials Java 8 Tutorial Java 8 Parallel Stream
Secondary Sidebar
Java 8 Tutorial
  • Java 8 basic
    • Java 8 forEach
    • Java 8 Documentation
    • Java 8 Method Reference
    • Java 8 List to Map
    • Java 8 Parallel Stream
    • Java 8 Functional Interface
    • Java 8 API
    • Java 8 Lambda
    • Java 8 Comparator
    • Java 8 Group By
    • Java 8 Predicate
    • Java 8 Default Methods
    • Java 8 Thread
    • Java 8 HashMap
    • Java 8 Read File
    • Java 8 OpenJDK
    • Java 8 Stream Filter
    • Java 8 Interface

Java 8 Parallel Stream

Introduction to Java 8 Parallel Stream

Java 8 parallel stream is a feature of java 8 and higher version in that we are utilizing cores of multiple processors. Normally java code contains a single stream of processing, which will execute sequentially. While using parallel streams, we can divide our code into many streams which execute code in parallel core and separate. The final result contains the combination of each outcome.

In our application, using parallel streams is recommended in cases where there is no issue with order execution, the result of our code is affected, and the state of a specified element is not affected. Parallel streams were introduced in Java 8 to improve program performance, but we believe that using parallel streams is the best option. In many cases, we need our code to be executed in a specific order; in these cases, we use a parallel stream.

The performance difference between sequential and parallel streams is only relevant to large-scale programs or applications. When the sequential stream fails to function properly, we can use the parallel stream in our project. There are two ways in which we are creating the parallel stream using the parallel method on a stream and parallelStream on collection.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Java 8 Parallel Stream

Key Takeaways

  • In java 8 new concept is introduced named as a parallel stream which was used for parallel processing. We can process multiple threads parallel by using a parallel stream.
  • The parallel method onto the base interface is returning a parallel stream, which was equivalent to the method of ParallelStream.

How to Create Java 8 Parallel Stream?

We can create a parallel stream by using two methods. Below methods shows how we can create the parallel stream as follows:

  • By using parallel method onto the stream: The parallel method on the base interface returns an equivalent parallel stream. In the code below, we create a file object that points to the pre-existing txt file system. Then we’re creating the stream that was reading from the txt file one line at a time, and then we’re using parallel methods to read the print file onto the console. The execution order is different on each run; we can check the same in the output.
  • By using parallel stream onto the collection: The parallel stream methods onto the collection interface return the parallel stream by using the collection as a source. In the below example, we are giving the parallel stream by using the text file. We are using example.txt file to read the data from the file.

Java 8 Parallel Stream Processing

At the time of increasing the performance of the program, we are using the parallel stream in our application. By using parallel processing, it is very easy to create the stream which executes parallel and makes the multiple cores of processing. The processing is faster while dividing the work into multiple cores.

The parallel stream processing in java 8 is a wrapper around of the data source, which allows the bulk operations on data on which we are working. It is a very convenient way to divide our operation in multiple threads.

Below example shows parallel stream processing as follows:

Code:

import java.util.stream.IntStream;
public class ParallelStream {
public static void main(String[] args) {
System.out.println ("Normal Execution");
IntStream ra = IntStream.rangeClosed (1, 5);
ra.forEach (System.out::println);
System.out.println ("Parallel Execution");
IntStream ra2 = IntStream.rangeClosed (1, 5);
ra2.parallel ().forEach(System.out::println);
}
}

Output:

Java 8 Parallel Stream Processing

In the below example, we are running the stream in a parallel mode as follows:

We are using the normal and parallel execution as follows:

Code:

import java.util.stream.IntStream;
public class ParallelStream {
public static void main(String[] args) {
System.out.println ("Normal Execution");
IntStream ra = IntStream.rangeClosed (1, 5);
System.out.println (ra.isParallel());
ra.forEach (System.out::println);
System.out.println ("Parallel Execution");
IntStream ra2 = IntStream.rangeClosed (1, 5);
IntStream range2Parallel = ra2.parallel ();
System.out.println (range2Parallel.isParallel ());
range2Parallel.forEach (System.out::println);
}
}

Output:

normal and parallel execution

Examples of Java 8 Parallel Stream

Given below are the examples mentioned:

Example #1

In the below example, we are using the parallel method on stream and using Example.txt file to get the output as follows.

Code:

import java.io.IOException;
import java.util.stream.Stream;
import java.io.File;
import java.nio.file.Files;
public class Example
{
public static void main(String[] args) throws IOException
{
File f = new File("C:\\Users\\OMSAI\\Desktop\\Example.txt");
Stream<String> text = Files.lines (f.toPath());
text.parallel ().forEach(System.out::println);
text.close ();
}
}

Output:

Java 8 Parallel Stream Method

Example #2

In the below example, we are using parallelStream method on stream and using Example.txt file to get the output as follows.

Code:

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.*;
public class Example {
public static void main(String[] args) throws IOException
{
File f
= new File("C:\\Users\\OMSAI\\Desktop\\Example.txt");
List<String> t
= Files.readAllLines (f.toPath());
t.parallelStream ().forEach(System.out::println);
}
}

Output:

using Example.txt file

FAQs

Given below are the FAQs mentioned:

Q1. What is the use of java 8 parallel stream?

Answer: Java 8 parallel stream is used to increase the performance of our application. It is recommended to use the parallel stream in our application.

Q2. Which method we are using to create a parallel stream?

Answer: We are using two methods for creating the parallel stream i.e. parallel method and the parallel stream method.

Q3. Why we are using the parallel stream in our application?

Answer: We are using parallel stream in our application to increase the performance of our application. Use a parallel stream is the best choice for our application.

Conclusion

The performance difference between sequential and parallel stream is only concerned with large-scale programs or large applications. Java 8 parallel stream is a feature of java 8 and higher version in that we are utilizing cores of multiple processors. Normally java code will contain a single stream of processing, where it will execute sequentially.

Recommended Articles

This is a guide to Java 8 Parallel Stream. Here we discuss the introduction, how to create java 8 parallel stream, and examples. You may also have a look at the following articles to learn more –

  1. Text File in Java
  2. Java 8 forEach
  3. Java for Automation Testing
  4. Java SFTP
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more