Skip to content

Instantly share code, notes, and snippets.

@lrlucena
Created May 2, 2019 17:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lrlucena/fb8fe0d08f2614b12f94d203d797781c to your computer and use it in GitHub Desktop.
Save lrlucena/fb8fe0d08f2614b12f94d203d797781c to your computer and use it in GitHub Desktop.
import java.util.ArrayList;
import java.util.List;
public class ParallelStreamDemo {
public static void main(String[] args) {
long t1, t2;
List<Employee> eList = new ArrayList<Employee>();
for(int i=0; i<100; i++) {
eList.add(new Employee("A", 20000));
eList.add(new Employee("B", 3000));
eList.add(new Employee("C", 15002));
eList.add(new Employee("D", 7856));
eList.add(new Employee("E", 200));
eList.add(new Employee("F", 50000));
}
/***** Here We Are Creating A 'Sequential Stream' & Displaying The Result *****/
t1 = System.currentTimeMillis();
System.out.println("Sequential Stream Count?= " + eList.stream().filter(e -> e.getSalary() > 15000).count());
t2 = System.currentTimeMillis();
System.out.println("Sequential Stream Time Taken?= " + (t2-t1) + "\n");
/***** Here We Are Creating A 'Parallel Stream' & Displaying The Result *****/
t1 = System.currentTimeMillis();
System.out.println("Parallel Stream Count?= " + eList.parallelStream().filter(e -> e.getSalary() > 15000).count());
t2 = System.currentTimeMillis();
System.out.println("Parallel Stream Time Taken?= " + (t2-t1));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment