Click here to watch in Youtube :
https://www.youtube.com/watch?v=fAAT--78-MI&list=UUhwKlOVR041tngjerWxVccw
Student.java
Click the below link to download the code:https://www.youtube.com/watch?v=fAAT--78-MI&list=UUhwKlOVR041tngjerWxVccw
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; } @Override public String toString() { return this.name + " - " + this.score; } @Override public int compareTo(Student another) { return another.getScore() - this.score; } }NonStreamDemo.java
import java.util.ArrayList; import java.util.List; public class NonStreamDemo { 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)); /* * Find students whose score >= 80 */ List<Student> goodStudentList = new ArrayList<>(); for (Student student : studentList) { if (student.getScore() >= 80) { goodStudentList.add(student); } } for (Student student : goodStudentList) { System.out.println(student); } /* * Calculate average score of all students */ double sum = 0.0; for (Student student : studentList) { sum += student.getScore(); } double average = sum / studentList.size(); System.out.println("Average score: " + average); } }Output
Alice - 82 Bob - 90 David - 80 Gary - 88 Henry - 98 Average score: 72.7StreamDemo.java
import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; 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)); /* * Find students whose score >= 80 */ List<Student> goodStudentList = studentList.stream() .filter(s -> s.getScore() >= 80) .collect(Collectors.toList()); goodStudentList.stream().forEach(System.out::println); System.out.println("-------------------------------"); /* * Calculate average score of all students */ double average = studentList.stream() .mapToInt(s -> s.getScore()).average().getAsDouble(); System.out.println("Average score: " + average); System.out.println("-------------------------------"); /* * Find top 3 students. * * As you can see, the operations on a stream can be chained * together (intermediate operations) and end with a terminal * operation. Such a chain of stream operations is called * stream pipeline. */ List<Student> top3Students = studentList.stream() .filter(s -> s.getScore() >= 80).sorted().limit(3) .collect(Collectors.toList()); System.out.println("Top 3 Students by Score:"); top3Students.forEach(s -> System.out.println(s)); } }Output
Alice - 82 Bob - 90 David - 80 Gary - 88 Henry - 98 ------------------------------- Average score: 72.7 ------------------------------- Top 3 Students by Score: Henry - 98 Bob - 90 Gary - 88
https://sites.google.com/site/ramj2eev1/home/javabasics/StreamDemo_Student_score_top3.zip?attredirects=0&d=1
Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/StreamDemo_Student_score_top3
Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/adf72ea7a150b1a34af5820c19f2232b522614b9/BasicJava/StreamDemo_Student_score_top3/?at=master
See also:
Excellent Blog Content.Thanks for sharing information.
ReplyDeleteGCP Data Engineer Online Training
GCP Online Training
Google Cloud Data Engineer Training
GCP Data Engineer Training in Ameerpet
GCP Training in Hyderabad
Google Cloud Platform Training in Hyderabad
GCP Training in Ameerpet
Google Cloud Training Institute in Hyderabad