Skip to content

Commit 96623f2

Browse files
committed
Refactoring for Method Reference
1 parent f072185 commit 96623f2

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

Modern-Java-Examples/src/com/learn/data/StudentDataBase.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,16 @@
33
import java.util.ArrayList;
44
import java.util.Arrays;
55
import java.util.List;
6+
import java.util.function.Supplier;
67

78
public class StudentDataBase {
89

10+
11+
/**
12+
* Utility Method to return 1 Student.
13+
*/
14+
public static Supplier<Student> studentSupplier = () -> new Student("Adam", 2, "male", 4.5, Arrays.asList("swimming", "basketball"));
15+
916
/**
1017
* Dummy Student Data
1118
* @return
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.learn.methodreference;
2+
3+
import com.learn.data.Student;
4+
import com.learn.data.StudentDataBase;
5+
6+
import java.util.function.Predicate;
7+
8+
public class RefactorMethodReferenceExample {
9+
10+
static Predicate<Student> studentPredicate = (student) -> student.getGradeLevel() >= 3;
11+
12+
static Predicate<Student> predicateWithMethodReference = RefactorMethodReferenceExample::gradeLevelGreaterThanThree;
13+
14+
public static void main(String[] args) {
15+
16+
System.out.println(studentPredicate.test(StudentDataBase.studentSupplier.get()));
17+
18+
System.out.println(predicateWithMethodReference.test(StudentDataBase.studentSupplier.get()));
19+
}
20+
21+
public static boolean gradeLevelGreaterThanThree(Student student) {
22+
return student.getGradeLevel() >= 3;
23+
}
24+
}

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ This repository contains the basic &amp; advance level examples related to Java
1717
* [BinaryOperator](Modern-Java-Examples/src/com/learn/functionalInterfaces/BinaryOperatorExample.java)
1818
* [Supplier](Modern-Java-Examples/src/com/learn/functionalInterfaces/SupplierExample.java)
1919
* [Method Reference](Modern-Java-Examples/src/com/learn/methodreference/FunctionMethodReferenceExample.java)
20+
* [Consumer Method Reference](Modern-Java-Examples/src/com/learn/methodreference/ConsumerMethodReferenceExample.java)
21+
* [Refactoring Method Reference](Modern-Java-Examples/src/com/learn/methodreference/RefactorMethodReferenceExample.java)
2022

2123

2224

0 commit comments

Comments
 (0)