Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 486f1dc

Browse files
[yuvrajs] Method References and Operations on Custom Class
1 parent 2439ef6 commit 486f1dc

File tree

3 files changed

+179
-1
lines changed

3 files changed

+179
-1
lines changed

‎README.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,53 @@ Functional programming is a paradigm that allows programming using expressions i
1919

2020

2121
## Implementations using Functional Programming
22+
2223
### Using Streams, Filters and Lambdas:
2324
* [Print each element from list in its own line](/src/main/java/com/ysingh/functional/FP01PrintListElement.java)
2425
* [Print courses, courses having name with Spring, courses having name greater than 3 characters](src/main/java/com/ysingh/functional/FP02CourseExcerise.java)
26+
2527
### Using Map:
2628
* [Print Square of each number in the list](src/main/java/com/ysingh/functional/FP03PrintSquareOfEachListElement.java)
29+
2730
### Using Reduce:
2831
* [Print sum of all elements in the list](src/main/java/com/ysingh/functional/FP04AddNumbersInList.java)
2932
* [Print max and min number from all elements in the list](src/main/java/com/ysingh/functional/FP05MaximumAndMinimumNumberInList.java)
3033
* [Square every number in a list and find the sum of squares](src/main/java/com/ysingh/functional/FP06Excersise.java)
3134
* [Cube every number in a list and find the sum of cubes](src/main/java/com/ysingh/functional/FP06Excersise.java)
3235
* [Find sum of odd numbers in a list](src/main/java/com/ysingh/functional/FP06Excersise.java)
36+
3337
### Using Distinct and Sorted:
3438
* [Print distinct numbers in the list](src/main/java/com/ysingh/functional/FP07DistinctAndSorted.java)
3539
* [Sort list of numbers and print it](src/main/java/com/ysingh/functional/FP07DistinctAndSorted.java)
3640
* [Sort list of strings and print it](src/main/java/com/ysingh/functional/FP07DistinctAndSorted.java)
41+
3742
### Using Collect:
3843
* [Square each element of list and collect it as new list](src/main/java/com/ysingh/functional/FP08Collect.java)
3944
* [Find all even element of list and collect it as new list](src/main/java/com/ysingh/functional/FP08Collect.java)
4045
* [Find length of each course element of list and collect it as new list](src/main/java/com/ysingh/functional/FP08Collect.java)
46+
47+
### Using allMatch, noneMatch, anyMatch:
48+
* [Example](src/main/java/com/ysingh/functional/FP12CustomClass.java)
49+
50+
### Using Comparator:
51+
* [Example](src/main/java/com/ysingh/functional/FP12CustomClass.java)
52+
53+
### Using skip, limit:
54+
* [Example](src/main/java/com/ysingh/functional/FP12CustomClass.java)
55+
56+
### Using max, min, findFirst, findAny:
57+
* [Example](src/main/java/com/ysingh/functional/FP12CustomClass.java)
58+
59+
### Using sum, average, count:
60+
* [Example](src/main/java/com/ysingh/functional/FP12CustomClass.java)
61+
62+
### Using groupingBy:
63+
* [Example](src/main/java/com/ysingh/functional/FP12CustomClass.java)
64+
4165
### Terminal or Intermediate operations:
4266
* Function or method which returns another stream of element called as Intermediate functions or methods or operations.
4367
* Function or method which does not return another stream of element called as Terminal functions or methods or operations.
68+
4469
### Functional Interfaces:
4570
A functional interface has exactly one abstract method.
4671
* **Predicate\<T\>:** Represents a predicate (boolean-valued function) of one argument.
@@ -77,6 +102,11 @@ A functional interface has exactly one abstract method.
77102
* [Example](src/main/java/com/ysingh/functional/FP09FunctionalInterface.java)
78103
* **IntUnaryOperator:** Represents an operation on a single int-valued operand that produces an int-valued result.
79104
* [Example](src/main/java/com/ysingh/functional/FP09FunctionalInterface.java)
105+
80106
### Behavior Parameterization:
81107
Passing the behaviour as parameter or argument to a method is called as Behavior Parameterization.
82-
* [Example](src/main/java/com/ysingh/functional/FP10BehaviourParameterization.java)
108+
* [Example](src/main/java/com/ysingh/functional/FP10BehaviourParameterization.java)
109+
110+
### Method Reference:
111+
Method reference is used to refer method of functional interface. The Method reference can only be used to replace a single method of lambda expression.
112+
* [Example](src/main/java/com/ysingh/functional/FP11MethodReferences.java)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.ysingh.functional;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
import java.util.function.Supplier;
6+
7+
public class FP11MethodReferences {
8+
9+
public static void main(String[] args) {
10+
List<String> courses = Arrays.asList("Spring", "Spring Boot", "API", "Microservices", "AWS", "PCF", "Azure", "Docker", "Kubernetes");
11+
12+
//Method Reference on Static Method
13+
courses.stream().map(str -> str.toUpperCase()).forEach(System.out::println);
14+
System.out.println("---------------");
15+
16+
//Method Reference on Object of the class
17+
courses.stream().map(String::toUpperCase).forEach(System.out::println);
18+
19+
System.out.println("---------------");
20+
Supplier<String> supplier = () -> new String();
21+
System.out.println("Empty String 1: "+supplier.get());
22+
23+
//Method Reference on Constructor of the class
24+
Supplier<String> supplier1 = String::new;
25+
System.out.println("Empty String 2: "+supplier1.get());
26+
27+
}
28+
29+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /