Monday 25 September 2017

How Lambda expressions used in Streams | Java 8 streams | Streams in Java 8


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

Click the below Image to Enlarge
How Lambda expressions used in Streams | 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> listStudents = new ArrayList<>();

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

        listStudents.stream().sorted().filter(s -> s.getScore() >= 70)
                .forEach(System.out::println);

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

        listStudents.stream().filter(s -> s.getScore() >= 70)
                .map(s -> s.getName()).sorted()
                .forEach(name -> System.out.println(name));
    }

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

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

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

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/5eb462211ac2fd9fb1bace2049e170a49c5caa69/BasicJava/StreamDemo_StreamAndLambda/?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