0

Two bellow snippets with the same output, one with using Functional interface and lambda expression, while the other snippet uses two simple method and easy to understand. What are the benefits first snippet code while it increases complexity.

First:

interface StringFunction {
    String run(String str);
}

public class Main {
    public static void main(String[] args) {
        StringFunction exclaim = (s) -> s + "!";
        StringFunction ask = (s) -> s + "?";
        printFormatted("Hello", exclaim);
        printFormatted("Hello", ask);
    }
    public static void printFormatted(String str, StringFunction format) {
        String result = format.run(str);
        System.out.println(result);
    }
}

Second:

public class Main {
    public static void main(String[] args) {
        System.out.println(exclaim("Hello"));
        System.out.println(ask("Hello"));
    }

    static String exclaim(String s) {
        return s + "!";
    }

    static String ask(String s) {
        return s + "?";
    }
}
8
  • 1
    With that small example code, there is no benefit at all. Commented Jan 26, 2021 at 12:17
  • These code examples look like do the same job. But actually, they are completely different from each other. Second one's methods(exclaim, ask) get String value on parameter, but in first one you are passing a functional interface that gets String and returns String. So, functional interfaces may be practical usage on some use cases. Commented Jan 26, 2021 at 12:25
  • @Andreas It would be better if you explain why there is no benefit! the second code do the same thing with simple and easy way. Commented Jan 26, 2021 at 12:33
  • @İlkerKorkut I want a clear explanation, why should I define SAM interface then implement and pass into method argument to get the same result as Second code? Commented Jan 26, 2021 at 12:36
  • Ok. Explanation: There is no benefit because the second code does the same thing in an easier way. --- Don't know why you wanted me to explain that, since you already said it. Commented Jan 26, 2021 at 12:36

2 Answers 2

2

In the first example, you are passing a behaviour which you can change for every call in a much cleaner and succinct way than your second way e.g.

StringFunction exclaim = s -> s + "!";
printFormatted("Hello", exclaim);

exclaim = s -> s + "!!!";
printFormatted("Hello", exclaim);

If you have to do it in your second way, you will have to write another method for it.

On a side note, in your lambda expression, you do not need the parenthesis in case of a single parameter i.e. you can write it simply as (I've already done this way in the code above):

StringFunction exclaim = s -> s + "!";
StringFunction ask = s -> s + "?";
Sign up to request clarification or add additional context in comments.

Comments

1

Biggest advantage of lambda expressions is they are focused on expression instead of object's state.

That means you specify exactly what you wants without dealing with object you pass there. This allows you to use any object and get result you are expecting.

After all lambdas are still interfaces defining behavior as any other interfaces and depends on their implementation in concrete class.

Your provided code snippet is missing bigger complexity so lambda seems to be overkill there. But in complex systems it supports better maintainability and get rid of tight coupling

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.