package javaFeatures;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
public class MethodReferences {
public int add(int a, int b) {
return a + b;
}
public int mul(int a, int b) {
return a * b;
}
public String lower(String a) {
return a.toLowerCase();
}
public void printDate(Date date) {
System.out.println(date);
}
public static boolean check(int value) {
if(value > 100) {
System.out.println("Value is greater than 100...");
return true;
}
else {
System.out.println("Value is lesser than 100...");
return false;
}
}
public void predicating(Predicate<Integer> pred, int a) {
System.out.println("Before usage of Predicate with changed value...");
a = 200;
pred.test(a);
}
public void operBinary(BinaryOperator<Integer> operator, int a, int b) {
System.out.println("About to apply the opration with changed values...");
a = 5;
b = 6;
System.out.println(operator.apply(a, b));
}
public void operS(Function<String, String> stringOperator, String a) {
System.out.println(stringOperator.apply(a));
}
public String operC(Supplier<GregorianCalendar> supplier) {
return "Current date applied : " + supplier.get().getTime();
}
/**
* In Below method both ways are shown, method reference & lambda expression
*/
public static void main(String[] args) {
MethodReferences mr = new MethodReferences();
mr.operBinary((a, b) -> mr.mul(a, b), 1, 3);
// mr.predicating((a) -> mr.check(a), 10);
mr.predicating(MethodReferences::check, 10);
// mr.operS(s -> s.toLowerCase(), "HELLO");
mr.operS(mr::lower, "HELLO");
// System.out.println(mr.operC(() -> new GregorianCalendar()));
System.out.println(mr.operC(GregorianCalendar::new));
}
}
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
public class MethodReferences {
public int add(int a, int b) {
return a + b;
}
public int mul(int a, int b) {
return a * b;
}
public String lower(String a) {
return a.toLowerCase();
}
public void printDate(Date date) {
System.out.println(date);
}
public static boolean check(int value) {
if(value > 100) {
System.out.println("Value is greater than 100...");
return true;
}
else {
System.out.println("Value is lesser than 100...");
return false;
}
}
public void predicating(Predicate<Integer> pred, int a) {
System.out.println("Before usage of Predicate with changed value...");
a = 200;
pred.test(a);
}
public void operBinary(BinaryOperator<Integer> operator, int a, int b) {
System.out.println("About to apply the opration with changed values...");
a = 5;
b = 6;
System.out.println(operator.apply(a, b));
}
public void operS(Function<String, String> stringOperator, String a) {
System.out.println(stringOperator.apply(a));
}
public String operC(Supplier<GregorianCalendar> supplier) {
return "Current date applied : " + supplier.get().getTime();
}
/**
* In Below method both ways are shown, method reference & lambda expression
*/
public static void main(String[] args) {
MethodReferences mr = new MethodReferences();
mr.operBinary((a, b) -> mr.mul(a, b), 1, 3);
// mr.predicating((a) -> mr.check(a), 10);
mr.predicating(MethodReferences::check, 10);
// mr.operS(s -> s.toLowerCase(), "HELLO");
mr.operS(mr::lower, "HELLO");
// System.out.println(mr.operC(() -> new GregorianCalendar()));
System.out.println(mr.operC(GregorianCalendar::new));
}
}