|
| 1 | +package com.ysingh.functional; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Comparator; |
| 5 | +import java.util.List; |
| 6 | +import java.util.function.Predicate; |
| 7 | +import java.util.stream.Collectors; |
| 8 | + |
| 9 | +class Course { |
| 10 | + private String name; |
| 11 | + private String category; |
| 12 | + private int reviewScore; |
| 13 | + private int noOfStudents; |
| 14 | + |
| 15 | + public Course(String name, String category, int reviewScore, int noOfStudents) { |
| 16 | + super(); |
| 17 | + this.name = name; |
| 18 | + this.category = category; |
| 19 | + this.reviewScore = reviewScore; |
| 20 | + this.noOfStudents = noOfStudents; |
| 21 | + } |
| 22 | + |
| 23 | + public String getName() { |
| 24 | + return name; |
| 25 | + } |
| 26 | + |
| 27 | + public void setName(String name) { |
| 28 | + this.name = name; |
| 29 | + } |
| 30 | + |
| 31 | + public String getCategory() { |
| 32 | + return category; |
| 33 | + } |
| 34 | + |
| 35 | + public void setCategory(String category) { |
| 36 | + this.category = category; |
| 37 | + } |
| 38 | + |
| 39 | + public int getReviewScore() { |
| 40 | + return reviewScore; |
| 41 | + } |
| 42 | + |
| 43 | + public void setReviewScore(int reviewScore) { |
| 44 | + this.reviewScore = reviewScore; |
| 45 | + } |
| 46 | + |
| 47 | + public int getNoOfStudents() { |
| 48 | + return noOfStudents; |
| 49 | + } |
| 50 | + |
| 51 | + public void setNoOfStudents(int noOfStudents) { |
| 52 | + this.noOfStudents = noOfStudents; |
| 53 | + } |
| 54 | + |
| 55 | + public String toString() { |
| 56 | + return name + ":" + noOfStudents + ":" + reviewScore; |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +public class FP12CustomClass { |
| 61 | + |
| 62 | + public static void main(String[] args) { |
| 63 | + |
| 64 | + List<Course> courses = new ArrayList<>(); |
| 65 | + courses.add(new Course("Spring", "Framework", 98, 20000)); |
| 66 | + courses.add(new Course("Spring Boot", "Framework", 95, 18000)); |
| 67 | + courses.add(new Course("API", "Microservices", 97, 22000)); |
| 68 | + courses.add(new Course("Microservices", "Microservices", 96, 25000)); |
| 69 | + courses.add(new Course("FullStack", "FullStack", 91, 14000)); |
| 70 | + courses.add(new Course("AWS", "Cloud", 92, 21000)); |
| 71 | + courses.add(new Course("Azure", "Cloud", 99, 21000)); |
| 72 | + courses.add(new Course("Docker", "Cloud", 92, 20000)); |
| 73 | + courses.add(new Course("Kubernetes", "Cloud", 91, 20000)); |
| 74 | + |
| 75 | + Predicate<Course> reviewScoreGreaterThan90Predicate = course -> course.getReviewScore() > 90; |
| 76 | + Predicate<Course> reviewScoreGreaterThan95Predicate = course -> course.getReviewScore() > 95; |
| 77 | + Predicate<Course> reviewScoreLessThan90Predicate = course -> course.getReviewScore() < 90; |
| 78 | + |
| 79 | + System.out.println("All Match - reviewScoreGreaterThan90Predicate : "+courses.stream().allMatch(reviewScoreGreaterThan90Predicate)); |
| 80 | + System.out.println("All Match - reviewScoreGreaterThan95Predicate : "+courses.stream().allMatch(reviewScoreGreaterThan95Predicate)); |
| 81 | + System.out.println("None Match - reviewScoreLessThan90Predicate : "+courses.stream().noneMatch(reviewScoreLessThan90Predicate)); |
| 82 | + System.out.println("Any Match - reviewScoreLessThan90Predicate : "+courses.stream().anyMatch(reviewScoreLessThan90Predicate)); |
| 83 | + System.out.println("Any Match - reviewScoreGreaterThan95Predicate : "+courses.stream().anyMatch(reviewScoreGreaterThan95Predicate)); |
| 84 | + |
| 85 | + Comparator<Course> comparingByNoOfStudentsIncreasing = Comparator.comparingInt(Course::getNoOfStudents); |
| 86 | + System.out.println(courses.stream().sorted(comparingByNoOfStudentsIncreasing).collect(Collectors.toList())); |
| 87 | + |
| 88 | + Comparator<Course> comparingByNoOfStudentsDecreasing = Comparator.comparingInt(Course::getNoOfStudents).reversed(); |
| 89 | + System.out.println(courses.stream().sorted(comparingByNoOfStudentsDecreasing).collect(Collectors.toList())); |
| 90 | + |
| 91 | + Comparator<Course> comparingByNoOfStudentsAndNoOfReviews = Comparator.comparingInt(Course::getNoOfStudents).thenComparingInt(Course::getReviewScore).reversed(); |
| 92 | + System.out.println(courses.stream().sorted(comparingByNoOfStudentsAndNoOfReviews).collect(Collectors.toList())); |
| 93 | + |
| 94 | + System.out.println(courses.stream().sorted(comparingByNoOfStudentsAndNoOfReviews).limit(5).collect(Collectors.toList())); |
| 95 | + System.out.println(courses.stream().sorted(comparingByNoOfStudentsAndNoOfReviews).skip(3).collect(Collectors.toList())); |
| 96 | + System.out.println(courses.stream().sorted(comparingByNoOfStudentsAndNoOfReviews).skip(3).limit(5).collect(Collectors.toList())); |
| 97 | + |
| 98 | + System.out.println(courses.stream().max(comparingByNoOfStudentsAndNoOfReviews)); |
| 99 | + System.out.println(courses.stream().min(comparingByNoOfStudentsAndNoOfReviews)); |
| 100 | + System.out.println(courses.stream().filter(reviewScoreLessThan90Predicate).min(comparingByNoOfStudentsAndNoOfReviews).orElse(new Course("Kubernetes", "Cloud", 91, 20000))); |
| 101 | + System.out.println(courses.stream().min(comparingByNoOfStudentsAndNoOfReviews).orElse(new Course("Kubernetes", "Cloud", 91, 20000))); |
| 102 | + |
| 103 | + System.out.println(courses.stream().filter(reviewScoreGreaterThan95Predicate).findFirst()); |
| 104 | + System.out.println(courses.stream().filter(reviewScoreGreaterThan95Predicate).findAny()); |
| 105 | + |
| 106 | + System.out.println(courses.stream().filter(reviewScoreGreaterThan95Predicate).mapToInt(course -> course.getNoOfStudents()).sum()); |
| 107 | + System.out.println(courses.stream().filter(reviewScoreGreaterThan95Predicate).mapToInt(course -> course.getNoOfStudents()).average()); |
| 108 | + System.out.println(courses.stream().filter(reviewScoreGreaterThan95Predicate).mapToInt(course -> course.getNoOfStudents()).count()); |
| 109 | + System.out.println(courses.stream().filter(reviewScoreGreaterThan95Predicate).mapToInt(course -> course.getNoOfStudents()).max()); |
| 110 | + System.out.println(courses.stream().filter(reviewScoreGreaterThan95Predicate).mapToInt(course -> course.getNoOfStudents()).min()); |
| 111 | + |
| 112 | + System.out.println(courses.stream().collect(Collectors.groupingBy(Course::getCategory))); |
| 113 | + System.out.println(courses.stream().collect(Collectors.groupingBy(Course::getCategory, Collectors.counting()))); |
| 114 | + System.out.println(courses.stream().collect(Collectors.groupingBy(Course::getCategory, Collectors.maxBy(Comparator.comparing(Course::getReviewScore))))); |
| 115 | + System.out.println(courses.stream().collect(Collectors.groupingBy(Course::getCategory, Collectors.mapping(Course::getName, Collectors.toList())))); |
| 116 | + |
| 117 | + } |
| 118 | + |
| 119 | +} |
0 commit comments