I want to declare a function which accepts 3 parameters and returns an custom object in return like this
public static returnResult leadRatingFunction(LeadMaster lead,JSONObject json,String str)
{
}
// Where returnResult and LeadMaster are custom objects
I have declared this function in functional interface as follows,
@SuppressWarnings("hiding")
@FunctionalInterface
interface Function<LeadMaster,JSONObject,String,returnResult>
{
public returnResult apply(LeadMaster lead,JSONObject jsonObject,String str);
}
I want to use this function as hash map value like this,
Map<String, Function<LeadMaster,JSONObject,String,returnResult>> commands = new HashMap<>();
commands.put("leadRating",res -> leadRatingFunction(input1 ,input2 ,input3) ) ;
But it is giving error as "Lambda expression's signature does not match the signature of the functional interface method apply(LeadMaster, JSONObject, String)"
Thank you
A lambda expression that matches Function<LeadMaster,JSONObject,String,returnResult>
, would require three arguments:
Map<String, Function<LeadMaster,JSONObject,String,returnResult>> commands = new HashMap<>();
commands.put("leadRating",(a,b,c) -> leadRatingFunction(a,b,c));
Alternately, as Lino commented, you can use a method reference:
commands.put("leadRating",YourClass::leadRatingFunction);
BTW, I'm not sure you want your Function<LeadMaster,JSONObject,String,returnResult>
interface to be generic, since you placed names of actual classes as generic type parameters.
If you want generic parameters, use generic names:
interface Function<A,B,C,D>
{
public D apply(A a ,B b , C c);
}
Otherwise, it doesn't have to be generic:
interface Function
{
public returnResult apply(LeadMaster lead,JSONObject jsonObject,String str);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With