Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lambdas in FunctionalInterfaces in Java

I am trying to make use of lambdas in Java but can't understand how it works at all. I created @FunctionalInterface like this:

@FunctionalInterface
public interface MyFunctionalInterface {
    String getString(String s);
}

now in my code I use the lambda as here:

MyFunctionalInterface function = (f) -> {
    Date d = new Date();
    return d.toString() + " " + person.name + " used fnc str";
};

Next, I want to make use of my function passing it into the constructor of another class and use it like this:

public SampleClass(MyFunctionalInterface function) {
    String tmp = "The person info: %s";
    this.result = String.format(tmp, function.getString(String.valueOf(function)));
}

Why I need to use it the valueOf() here? I thought that thanks for this I could use just function.getString()?

Output: Tue Sep 19 11:04:48 CEST 2017 John used fnc str

like image 567
shurrok Avatar asked Sep 19 '17 08:09

shurrok


People also ask

What are lambdas in Java?

A lambda expression is a short block of code which takes in parameters and returns a value. Lambda expressions are similar to methods, but they do not need a name and they can be implemented right in the body of a method.

Can we write lambda without functional interface?

You do not have to create a functional interface in order to create lambda function.

How do you call a functional interface using lambda?

Lambda expression provides implementation of functional interface. An interface which has only one abstract method is called functional interface. Java provides an anotation @FunctionalInterface, which is used to declare an interface as functional interface.

How lambda expression and functional interfaces are related?

A lambda expression (lambda) is a short-form replacement for an anonymous class. Lambdas simplify the use of interfaces that declare single abstract methods. Such interfaces are known as functional interfaces. A functional interface can define as many default and static methods as it requires.


1 Answers

Your getString method requires a String argument, so you can't call it without any argument.

That said, your lambda expression ignores that String argument and instead takes data from some person variable (which you didn't show where you declare it).

Perhaps your functional interface should take a Person argument instead:

@FunctionalInterface
public interface MyFunctionalInterface {
    String getString(Person p);
}

MyFunctionalInterface function = p -> {
    Date d = new Date();
    return d.toString() + " " + p.name + " used fnc str";
};

public SampleClass(MyFunctionalInterface function, Person person) {
    String tmp = "The person info: %s";
    this.result = String.format(tmp, function.getString(person));
}

Alternately, you can remove the argument from your functional interface's method:

@FunctionalInterface
public interface MyFunctionalInterface {
    String getString();
}

MyFunctionalInterface function = () -> {
    Date d = new Date();
    return d.toString() + " " + person.name + " used fnc str";
};

public SampleClass(MyFunctionalInterface function) {
    String tmp = "The person info: %s";
    this.result = String.format(tmp, function.getString());
}
like image 94
Eran Avatar answered Sep 24 '22 15:09

Eran