Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lambda expression's signature does not match the signature of the functional interface method apply

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

like image 809
AJN Avatar asked Oct 30 '17 12:10

AJN


1 Answers

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);
}
like image 145
Eran Avatar answered Sep 16 '22 11:09

Eran