Monday 25 September 2017

How Parallel Stream works | Java 8 streams tutorial | Java 8 streams | Streams in Java 8


Click here to watch in Youtube :
https://www.youtube.com/watch?v=s2WpigBSS84&list=UUhwKlOVR041tngjerWxVccw

Click the below Image to Enlarge
How Parallel Stream works | Java 8 streams tutorial | Java 8 streams | Streams in Java 8
Student.java
public class Student implements Comparable<Student> 
{
    private String name;
    private int score;

    public Student(String name, int score)
    {
        this.name = name;
        this.score = score;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public String getName()
    {
        return this.name;
    }

    public void setScore(int score)
    {
        this.score = score;
    }

    public int getScore()
    {
        return this.score;
    }

    public String toString()
    {
        return this.name + " - " + this.score;
    }

    public int compareTo(Student another)
    {
        return another.getScore() - this.score;
    }
}
StreamDemo.java
import java.util.ArrayList;
import java.util.List;

public class StreamDemo
{
    public static void main(String[] args)
    {
        List<Student> studentList = new ArrayList<>();

        studentList.add(new Student("Alice", 82));
        studentList.add(new Student("Bob", 90));
        studentList.add(new Student("Carol", 67));
        studentList.add(new Student("David", 80));
        studentList.add(new Student("Eric", 55));
        studentList.add(new Student("Frank", 49));
        studentList.add(new Student("Gary", 88));
        studentList.add(new Student("Henry", 98));
        studentList.add(new Student("Ivan", 66));
        studentList.add(new Student("John", 52));

        /*
         * The following code may execute stream operations in
         * parallel
         */
        studentList.parallelStream().filter(s -> s.getScore() >= 80)
                .sorted().limit(3).forEach(System.out::println);

        System.out.println("------------------------------");

        /*
         * The Collection’s stream() method returns a sequential
         * stream. We can convert a sequential stream to a parallel
         * stream by calling the parallel() method on the current
         * stream.
         * 
         * The following example shows a stream executes the sorted
         * operation sequentially, and then execute the filter
         * operation in parallel
         */
        studentList.stream().sorted().parallel()
                .filter(s -> s.getScore() >= 80)
                .forEach(System.out::println);

    }

}
Output
Bob - 90
Henry - 98
Gary - 88
------------------------------
Gary - 88
David - 80
Alice - 82
Bob - 90
Henry - 98

Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/StreamDemo_student_parallelstream.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/StreamDemo_student_parallelstream

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/5eb462211ac2fd9fb1bace2049e170a49c5caa69/BasicJava/StreamDemo_student_parallelstream/?at=master

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • No comments:

    Post a Comment