Click here to watch in Youtube :
https://www.youtube.com/watch?v=BpX6i2a74k0&list=UUhwKlOVR041tngjerWxVccw
Click the below Image to Enlarge
What are the different method reference types | Method reference in Java 8 | Java method reference |
What are the different method reference types | Method reference in Java 8 | Java method reference |
What is Constructor reference | Method reference in Java 8 | Java method reference |
@FunctionalInterface interface Message { String displayMessage(char[] chArray); } public class LambdaDemo { public static void main(String[] args) { /* * Note that, in this program strFunc() of FunctionalInterface * returns a reference of type String. In this program, the * expression "String::new" creates a constructor reference to * the String's constructor. */ Message message = String::new; char[] charArray = { 'P', 'e', 't', 'e', 'r' }; /* * Here the method StrFunc() takes a Character array as an * argument, so the parameterized constructor * String(char[] charArray) is referred here. * * So finally, when ever you call the method strFunc() on * FunctionalInterface reference, a String object is * constructed and returned. */ System.out.println(message.displayMessage(charArray)); } }Output
PeterClick the below link to download the code:
Instance method reference of an existing object| Method reference in Java 8 | Java method reference |
import java.util.function.Consumer; class Car { private String name; public Car(String name) { super(); this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } } public class Mechanic { public void fix(Car c) { System.out.println("Mechanic is fixing " + c.getName()); } public void execute(Car car, Consumer<Car> c) { c.accept(car); } }LambdaDemo.java
import java.util.function.Consumer; public class LambdaDemo { public static void main(String[] args) { final Mechanic mechanic = new Mechanic(); Car car = new Car("Honda Jazz"); // Using an anonymous class mechanic.execute(car, new Consumer<Car>() { public void accept(Car c) { mechanic.fix(c); } }); // Using a lambda expression mechanic.execute(car, c -> mechanic.fix(c)); // Using a method reference mechanic.execute(car, mechanic::fix); } }Output
Mechanic is fixing Honda Jazz Mechanic is fixing Honda Jazz Mechanic is fixing Honda JazzClick the below link to download the code:
Static Method reference - Number more than 50| Method reference in Java 8 | Java method reference |
import java.util.ArrayList; import java.util.List; import java.util.function.BiPredicate; class Numbers { public static boolean isMoreThanFifty(int n1, int n2) { return (n1 + n2) > 50; } public static List<Integer> findNumbers(List<Integer> list, BiPredicate<Integer, Integer> p) { List<Integer> newList = new ArrayList<>(); for (Integer i : list) { if (p.test(i, i + 10)) { newList.add(i); } } return newList; } }LambdaDemo.java
import java.util.Arrays; import java.util.List; import java.util.function.BiPredicate; public class LambdaDemo { public static void main(String[] args) { List<Integer> list = Arrays.asList(12, 5, 45, 18, 33, 24, 40); // Using an anonymous class List<Integer> listOfNumbers = Numbers.findNumbers(list, new BiPredicate<Integer, Integer>() { public boolean test(Integer i1, Integer i2) { return Numbers.isMoreThanFifty(i1, i2); } }); System.out.println("listOfNumbers using a anonymous class = "+listOfNumbers); // Using a lambda expression listOfNumbers =Numbers.findNumbers(list, (i1, i2) -> Numbers.isMoreThanFifty(i1, i2)); System.out.println("listOfNumbers using a lambda expression = "+listOfNumbers); // Using a method reference listOfNumbers = Numbers.findNumbers(list, Numbers::isMoreThanFifty); System.out.println("listOfNumbers using a method reference = "+listOfNumbers); } }Output
listOfNumbers using a anonymous class = [45, 33, 24, 40] listOfNumbers using a lambda expression = [45, 33, 24, 40] listOfNumbers using a method reference = [45, 33, 24, 40]Click the below link to download the code:
Static Method reference - Number less than 50| Method reference in Java 8 | Java method reference |
import java.util.ArrayList; import java.util.List; import java.util.function.Predicate; class Numbers { public static boolean isLessThanFifty(int number) { return number < 50; } public static List<Integer> findNumbers(List<Integer> list, Predicate<Integer> p) { List<Integer> newList = new ArrayList<>(); for (Integer i : list) { /* * test the number is less than 50 * then add to the newList. */ if (p.test(i)) { newList.add(i); } } return newList; } }LambdaDemo.java
import java.util.Arrays; import java.util.List; import java.util.function.Predicate; public class LambdaDemo { public static void main(String[] args) { List<Integer> list = Arrays.asList(12, 55, 75, 88, 24, 40); // Using an anonymous class List<Integer> listOfLessthanfiftyNumbers = Numbers.findNumbers(list, new Predicate<Integer>() { public boolean test(Integer i) { return Numbers.isLessThanFifty(i); } }); System.out.println("listOfLessthanfiftyNumbers - anonymous class = "+listOfLessthanfiftyNumbers); // Using a lambda expression listOfLessthanfiftyNumbers = Numbers.findNumbers(list, (i) -> Numbers.isLessThanFifty(i)); System.out.println("listOfLessthanfiftyNumbers - lambda expression = "+listOfLessthanfiftyNumbers); // Using a method reference listOfLessthanfiftyNumbers = Numbers.findNumbers(list, Numbers::isLessThanFifty); System.out.println("listOfLessthanfiftyNumbers - method reference = "+listOfLessthanfiftyNumbers); } }Output
listOfLessthanfiftyNumbers - anonymous class = [12, 24, 40] listOfLessthanfiftyNumbers - lambda expression = [12, 24, 40] listOfLessthanfiftyNumbers - method reference = [12, 24, 40]
Click the below link to download the code:
How to use Method reference in forEach method of List | Method reference in Java 8 |
How to use Method reference in forEach method of List | Method reference in Java 8 |
How to use Method reference in forEach method of List | Method reference in Java 8 |
import java.util.ArrayList; import java.util.List; public class MethodReferenceDemo { public static void main(String[] args) { List<String> nameList = new ArrayList<>(); nameList.add("Peter"); nameList.add("John"); nameList.add("Juli"); nameList.add("Stephan"); nameList.forEach(name -> System.out.println(name)); System.out.println("------------------------"); // method reference nameList.forEach(System.out::println); } }Output
Peter John Juli Stephan ------------------------ Peter John Juli StephanClick the below link to download the code:
How to refer a constructor using method reference | Method reference in Java 8 |
/** * * We are using functional interface Messageable and referring constructor with * the help of functional interface. * */ @FunctionalInterface interface Messageable { Message getMessage(String msg); } class Message { public Message(String msg) { System.out.print(msg); } } public class MethodReferenceDemo { public static void main(String[] args) { Messageable messageable = Message::new; messageable.getMessage("Hello"); } }Output
HelloClick the below link to download the code:
import java.util.function.BiFunction; /** * * We are using predefined functional interface BiFunction and * referring a non-static method add(int a, int b) to it's functional * method apply(Integer t,Integer u). * */ class Arithmetic { public int add(int a, int b) { return a + b; } } public class MethodReferenceDemo { public static void main(String[] args) { BiFunction<Integer, Integer, Integer> adder = new Arithmetic()::add; int result = adder.apply(200, 200); System.out.println(result); } }Output
400
Click the below link to download the code:/** * * We are using predefined functional interface Runnable and referring * a non-static method ThreadStatus() to it's functional method run(). * */ public class MethodReferenceDemo { public void ThreadStatus() { System.out.println("Thread is running..."); } public static void main(String[] args) { MethodReferenceDemo methodReferenceDemo = new MethodReferenceDemo(); Runnable runnable = methodReferenceDemo::ThreadStatus; Thread thread = new Thread(runnable); thread.start(); } }Output
Thread is running...Click the below link to download the code:
Non-Static Method reference - Dog | Method reference in Java 8 | Java method reference |
Non-Static Method reference - Dog | Method reference in Java 8 | Java method reference |
/** * * We have defined a functional interface Dog and referring a * non-static method dogEating() to it's functional method eat(). * */ @FunctionalInterface interface Dog { void eat(); } public class MethodReferenceDemo { public void dogEating() { System.out.println("Dog is eating chicken."); } public static void main(String[] args) { // Creating object MethodReferenceDemo methodReferenceDemo = new MethodReferenceDemo(); // Referring non-static method using reference Dog dog = methodReferenceDemo::dogEating; // Calling interface method dog.eat(); } }Output
Dog is eating chicken.Click the below link to download the code:
Static Method reference - BiFunction Overload methods | Method reference in Java 8 |
import java.util.function.BiFunction; /** * * We can also override static methods by referring methods. In the * following example, we have defined and overloaded three add * methods. * */ class Arithmetic { public static int add(int a, int b) { return a + b; } public static float add(int a, float b) { return a + b; } public static float add(float a, float b) { return a + b; } } public class MethodReferenceDemo { public static void main(String[] args) { BiFunction<Integer, Integer, Integer> adder1 = Arithmetic::add; BiFunction<Integer, Float, Float> adder2 = Arithmetic::add; BiFunction<Float, Float, Float> adder3 = Arithmetic::add; int result1 = adder1.apply(10, 20); System.out.println(result1); float result2 = adder2.apply(10, 20.3f); System.out.println(result2); float result3 = adder3.apply(10.6f, 20.8f); System.out.println(result3); } }Output
30 30.3 31.4Click the below link to download the code:
import java.util.function.BiFunction; /** * * We are using predefined functional interface BiFunction and * referring a static method add(int a, int b) to it's functional * method apply(Integer t,Integer u). * */ class Arithmetic { public static int add(int a, int b) { return a + b; } } public class MethodReferenceDemo { public static void main(String[] args) { BiFunction<Integer, Integer, Integer> adder = Arithmetic::add; int result = adder.apply(20, 20); System.out.println(result); } }Output
40
Click the below link to download the code:/** * * We are using predefined functional interface Runnable and referring * a static method ThreadStatus() to it's functional method run(). * */ public class MethodReferenceDemo { public static void ThreadStatus() { System.out.println("Thread is running..."); } public static void main(String[] args) { Runnable runnable = MethodReferenceDemo::ThreadStatus; Thread thread = new Thread(runnable); thread.start(); } }Output
Thread is running...Click the below link to download the code:
Static Method reference - Person| Method reference in Java 8 | Java method reference |
Static Method reference - Person| Method reference in Java 8 | Java method reference |
/** * * We have defined a functional interface person and referring a * static method personWalking() to it's functional method walk(). * */ @FunctionalInterface interface Person { void walk(); } public class MethodReferenceDemo { public static void personWalking() { System.out.println("Walking on the road."); } public static void main(String[] args) { // Referring static method Person person = MethodReferenceDemo::personWalking; // Calling interface method person.walk(); } }Output
Walking on the road.Click the below link to download the code: