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