Write a Java Program to pass method call as arguments to another method

Java is a powerful programming language that supports passing method calls as arguments to other methods.

This feature is called functional programming and allows for more concise and efficient code.

To pass a method call as an argument to another method, you need to define the method that accepts the method call as a parameter.

The method parameter should have a functional interface type that matches the signature of the method being passed.

For example, let’s say we have two methods, “add” and “subtract”, that perform addition and subtraction of two numbers.

We can pass the method call to these methods as arguments to another method that takes a functional interface as its parameter.

Here’s an example program that demonstrates this concept:

interface MathOperation {
    int operate(int a, int b);
}

public class Main {
    public static void main(String[] args) {
        int a = 10;
        int b = 5;
        int result = operate(a, b, (x, y) -> x + y); // pass addition method
        System.out.println("Addition result: " + result);
        result = operate(a, b, (x, y) -> x - y); // pass subtraction method
        System.out.println("Subtraction result: " + result);
    }

    public static int operate(int a, int b, MathOperation mathOperation) {
        return mathOperation.operate(a, b);
    }
}

In this example, we define a functional interface called “MathOperation” that has a single method called “operate”.

We then define a method called “operate” that takes two integers and a MathOperation object as its parameters.

This method calls the “operate” method on the MathOperation object and returns the result.

In the main method, we create two lambda expressions that represent the “add” and “subtract” methods.

We then call the “operate” method twice, passing the lambda expressions as arguments along with the two integers.

When we run this program, it will output the following:

Addition result: 15
Subtraction result: 5

As you can see, we were able to pass method calls as arguments to another method using lambda expressions.

This is just one example of how functional programming can make your code more concise and efficient.

Functional programming allows you to write more concise and expressive code by separating behavior from implementation.

It is particularly useful when working with collections and streams, where you need to perform operations on multiple elements at once.

Let’s say you have a list of numbers and you want to filter out the even numbers and then square the remaining odd numbers.

You can do this with the following code:

List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
List<Integer> result = numbers.stream()
    .filter(n -> n % 2 != 0)
    .map(n -> n * n)
    .collect(Collectors.toList());
System.out.println(result); // [1, 9, 25]

In this example, we first create a list of numbers using the “Arrays.asList” method.

We then create a stream from the list using the “stream” method.

We use the “filter” method to remove the even numbers from the stream by checking if the number modulo 2 is not equal to 0 (i.e., it’s odd).

We then use the “map” method to square the remaining odd numbers.

Finally, we use the “collect” method to collect the results into a new list.

Functional programming is a powerful technique that can help you write more maintainable and efficient code.

It encourages you to write functions that are smaller, more focused, and easier to reason about.

It also allows you to use higher-order functions, such as passing methods as arguments to other methods, which can make your code more flexible and reusable.


In conclusion, passing method calls as arguments to other methods is a powerful feature in Java’s functional programming paradigm.

It allows you to write more concise and efficient code, especially when working with collections and streams.

By understanding how to use functional interfaces and lambda expressions, you can take advantage of this feature to write more expressive and maintainable code.