JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

Java 8 Streams parallelism introduction.


In java, processing elements with an explicit for-loop is inherently serial processing. Streams facilitate parallel execution as well. All streams operations can execute either in serial or in parallel. The stream implementations in the JDK create serial streams unless parallelism is explicitly requested. For example, Collection has methods Collection.stream() and Collection.parallelStream(), which produce sequential and parallel streams respectively; other stream-bearing methods such as IntStream.range(int, int) produce sequential streams but these streams can be efficiently parallelized by invoking their BaseStream.parallel() method.

Stream pipeline serial or parallel execution id directly depends on the orientation of the stream on which it is invoked. Whether a stream will execute in serial or parallel can be determined with the isParallel() method, and the orientation of a stream can be modified with the BaseStream.sequential() and BaseStream.parallel() operations. When the terminal operation is initiated, the stream pipeline is executed sequentially or in parallel depending on the mode of the stream on which it is invoked.

Here is a simple example on Steam parallelism: The below example, combines filtering a list based on number of characters, converting them to upper case, sorting and finally printing them into single line of code, which is the power of streams.

package com.java2novice.streams;

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

public class ParallelStreamIntroEx {

	public static void main(String a[]) {

		List<String> vehicles = Arrays.asList("bus", "car", "bicycle", "flight", "train");

		vehicles.parallelStream().filter(str->str.length() > 3).map(String::toUpperCase).sorted().forEach(System.out::println);;
	}
}

Output:
BICYCLE
FLIGHT
TRAIN
<< Previous Program | Next Program >>

Java 8 Streams Examples

  1. How Java 8 Streams work?
  2. Java 8 Streams parallelism introduction.
  3. Explain non-interference behavior of Java 8 Streams.
  4. Create Java 8 Stream using Stream.of() method example.
  5. Create Java 8 Stream using List example.
  6. Create Java 8 Stream using Stream.generate() method.
  7. Java 8 Stream.filter() example.
  8. Java 8 Stream.map() example.
  9. Java 8 Stream flatmap method example.
  10. Java 8 Stream peek method example.
  11. Java 8 Stream distinct method example.
  12. Java 8 Stream sorted method example.
  13. Java 8 Stream limit method example.
  14. Java 8 Stream forEach method example.
  15. Java 8 Stream toArray method example.
  16. Java 8 Stream reduce method example.
  17. Java 8 Stream collect method example.
  18. Java 8 Stream concat method example.
  19. Java 8 Stream anyMatch(), allMatch() and noneMatch() example.
  20. Java 8 Stream findFirst(), findAny() example.
  21. Primitive type Stream example.
Knowledge Centre
Domain Naming Service(DNS)
It is very difficult to remember a set of numbers (IP address) to connect to the Internet. The Domain Naming Service(DNS) is used to overcome this problem. It maps one particular IP address to a string of characters. For example, www.java2novice.com implies com is the domain name reserved for US commercial sites, java2novice is the name of the company and www is the name of the specific computer, which is java2novice's server.
Famous Quotations
There is a great difference between worry and concern. A worried person sees a problem, and a concerned person solves a problem.
-- Harold Stephens

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2022 by Nataraja Gootooru. All Rights Reserved.