Wednesday, October 26, 2016

Problem No: #11 How to Print Stream of Collection type Using Parallel Stream?

Java 8 :: Streams and Lambda Expressions

/*
 * Problem No: #11
 * Problem Definition: How to Print Stream of Collection type Using Parallel Stream?
 * 
 * Answer: By Using, 
 * > java.util.Collection.stream(): Returns a sequential Stream with this collection as its source. 
 *  
 *> java.util.stream.Stream.forEach(): Performs an action for each element of this stream.This is a "terminal operation". 
 * 
 * **In the case of parallelStream () output differs from stream()
 * Program By: Mr.DIpak SOnar
 * Date : 26th Oct 2016
 */

import java.util.ArrayList;
import java.util.List;

public class ParallelStreamEx {

public static void main(String[] args) {
List<Integer> ListInt = new ArrayList<Integer>();

ListInt.add(21);
ListInt.add(111);
ListInt.add(189);
ListInt.add(3341);
ListInt.add(143);
ListInt.add(131);
ListInt.add(11);
ListInt.add(12);
ListInt.add(121);
ListInt.add(9919);

System.out.println("Printing Stream Using java.util.stream.Stream.forEach() method::");

// parallelStream () output differs from stream()
ListInt.parallelStream().forEach(System.out::println);

List<String> ListStr = new ArrayList<String>();

ListStr.add("Dipak");
ListStr.add("Amar");
ListStr.add("VishwaMitra");
ListStr.add("Naval");
ListStr.add("Alok");
ListStr.add("Virat");
ListStr.add("Pallav");
ListStr.add("Viral");
ListStr.add("Shail");
ListStr.add("Sonar");

System.out.println("Printing Stream Using java.util.stream.Stream.forEach() method::");

// parallelStream () output differs from stream()
ListStr.parallelStream().forEach(System.out::println);

}
}

No comments:

Post a Comment