Sunday 5 January 2020

How to pass method as a parameter in Java?


From Java8 onwards, you can pass a method as argument using lambda expressions and method references. To go in more details, let me explain about Functional interfaces.

Functional interface
A Functional interface is the interface, which contains only one abstract method. Java 8 uses functional interfaces to implement lambda expressions.

Some of the examples of functional interfaces
java.util.function.Predicate
java.util.function.Function
java.lang.Runnable

Lambda Expressions
Lambda expressions is a new feature included in Java SE 8. Lambda Expressions provide a way to represent functional interface using an expression.

Example
interface Arithmetic {
         void operation();
}

You can implement Operation interface like below.

Arithmetic add = (int a, int b) -> {
         return (a + b);
};

Similarly you can add other functionalities too.

Arithmetic multiply = (int a, int b) -> {
         return (a * b);
};

Arithmetic divide = (int a, int b) -> {
         return (a / b);
};

Arithmetic remainder = (int a, int b) -> {
         return (a % b);
};

App.java
package com.sample.app;

import java.io.IOException;

public class App {

 public interface Arithmetic {
  int operation(int var1, int var2);
 }

 
 public static void main(String args[]) throws IOException {
  Arithmetic add = (int a, int b) -> {
   return (a + b);
  };
  Arithmetic subtract = (int a, int b) -> {
   return (a - b);
  };
  Arithmetic multiply = (int a, int b) -> {
   return (a * b);
  };
  Arithmetic divide = (int a, int b) -> {
   return (a / b);
  };
  Arithmetic remainder = (int a, int b) -> {
   return (a % b);
  };

  System.out.println("Sum of 11 and 5 is " + add.operation(11, 5));
  System.out.println("Subtraction of 11 and 5 is " + subtract.operation(11, 5));
  System.out.println("Multiplication of 11 and 5 is " + multiply.operation(11, 5));
  System.out.println("Division of 11 and 5 is " + divide.operation(11, 5));
  System.out.println("Remainder of 11 and 5 is " + remainder.operation(11, 5));
 }
}


Run App.java, you can see below messages in console.
Sum of 11 and 5 is 16
Subtraction of 11 and 5 is 6
Multiplication of 11 and 5 is 55
Division of 11 and 5 is 2
Remainder of 11 and 5 is 1

How to pass a lambda as an argument to a function?
Define a method that takes functional interface as an argument.

public static void printOperationResult(Arithmetic method, int a, int b) {
         int result = method.operation(a, b);
         System.out.println("Result : " + result);
}

Now call the method printOperationResult, by passing lambda expression inline.

Example 1: Perform Addition
printOperationResult((int a, int b) -> {
         return (a + b);
}, 10, 20);

Example 1: Perform Subtraction
printOperationResult((int a, int b) -> {
         return (a - b);
}, 10, 20);

Example 1: Perform Multiplication
printOperationResult((int a, int b) -> {
         return (a * b);
}, 10, 20);

Example 1: Perform Division
printOperationResult((int a, int b) -> {
         return (a / b);
}, 10, 20);


App.java
package com.sample.app;

import java.io.IOException;

public class App {

 public interface Arithmetic {
  int operation(int var1, int var2);
 }

 public static void printOperationResult(Arithmetic method, int a, int b) {
  int result = method.operation(a, b);
  System.out.println("Result : " + result);
 }

 public static void main(String args[]) throws IOException {

  printOperationResult((int a, int b) -> {
   return (a + b);
  }, 10, 20);

  printOperationResult((int a, int b) -> {
   return (a - b);
  }, 10, 20);

  printOperationResult((int a, int b) -> {
   return (a * b);
  }, 10, 20);

  printOperationResult((int a, int b) -> {
   return (a / b);
  }, 10, 20);

 }
}

Method References
Method reference allows you to refer constructors or methods without executing them. You can access a method (or) constructor using :: notation. The only condition is that the methods should be assignable to any Functional Interface.

Example
System.out::println

Above statement refer println method.


App.java
package com.sample.app;

import java.util.Arrays;
import java.util.List;

public class App {

 public static void main(String args[]) {
  List<Integer> primes = Arrays.asList(2, 3, 5, 7, 11);

  primes.stream().forEach(System.out::println);

 }
}


Run App.java, you will see below messages in console.

2
3
5
7
11

Reference




You may like

No comments:

Post a Comment