Method Ref
-------------Ex:1----------------
package com.gs.iilp.corejava.java8;
interface Walkable {
public void walk();
}
public class MethodRef1 {
public static void tryWalking() {
System.out.println("Method ref 1 class trying walking ");
}
public static void tryWalking2() {
System.out.println("Method ref 1 class trying walking 2");
}
public static void main(String[] args) {
Walkable walkable = MethodRef1::tryWalking;
walkable.walk();
walkable = MethodRef1::tryWalking2;
walkable.walk();
}
}
-------------Ex:2----------------
package com.gs.iilp.corejava.java8;
interface Walkable {
public void walk();
}
public class MethodRef1 {
public static void tryWalking() {
System.out.println("Method ref 1 class trying walking ");
}
public static void tryWalking2(int param) {
System.out.println("Method ref 1 class trying walking "+ paramF);
}
public static void main(String[] args) {
Walkable walkable = MethodRef1::tryWalking;
walkable.walk();
walkable = MethodRef1::tryWalking2;
walkable.walk();
}
}
-------------Ex:3----------------
package com.gs.iilp.corejava.java8;
interface Walkable {
public void walk();
}
public class MethodRef1 {
public void tryWalkingThroughInstance() {
System.out.println("Method ref 1 class trying walking through instance var ");
}
public static void main(String[] args) {
Walkable walkable = new MethodRef1()::tryWalkingThroughInstance;
walkable.walk();
}
}
-------------Ex:4----------------
package com.gs.iilp.corejava.java8;
interface Walkable {
public void walk();
}
public class MethodRef1 {
public MethodRef1() {
System.out.println("Method ref 1 class trying walking through no args constructor ");
}
public static void main(String[] args) {
Walkable walkable = MethodRef1::new;
walkable.walk();
}
}
-------------Ex:5----------------
package com.gs.iilp.corejava.java8;
interface Walkable {
public void walk();
}
public class MethodRef1 {
public MethodRef1(String param) {
System.out.println("Method ref 1 class trying walking through 1 arg constructor with param " + param);
}
public static void main(String[] args) {
Walkable walkable = MethodRef1::new;
walkable.walk();
}
}
-------------Ex:6----------------
package com.gs.iilp.corejava.java8;
interface Walkable {
public void walk(String param);
}
public class MethodRef1 {
public MethodRef1(String param) {
System.out.println("Method ref 1 class trying walking through 1 arg constructor with param " + param);
}
public static void main(String[] args) {
Walkable walkable = MethodRef1::new;
walkable.walk("Sample param");
}
}
-------------Ex:7----------------
package com.gs.iilp.corejava.java8;
import java.util.function.BiFunction;
class Arithmetic {
public int add(int a, int b) {
return a + b;
}
}
public class MethodRef1 {
public static void main(String[] args) {
BiFunction<Integer, Integer, Integer> biFunction = new Arithmetic()::add;
int res = biFunction.apply(4, 6);
System.out.println("Res is " + res);
}
}
-------------Ex:8----------------
package com.gs.iilp.corejava.java8;
import java.util.function.BiFunction;
class Arithmetic {
public int add(int a, int b, int c) {
return a + b + c;
}
}
public class MethodRef1 {
public static void main(String[] args) {
BiFunction<Integer, Integer, Integer> biFunction = new Arithmetic()::add;
int res = biFunction.apply(4, 6);
System.out.println("Res is " + res);
}
}
Last updated