Method reference Purpose working in lamda

Hey. The question is simple.

What is the difference between and ? String::startWith

""::startWith

+3


source to share


1 answer


String::startWith

applies the method startWith()

to the first lambda argument.

""::startWith

applies a method startWith()

to a literal ""

or wider path to a variable that is not declared as a lambda argument.

To be more comprehensive, these two ways of providing a method reference are not overridden.

Let's say you want to use a method reference for a method public boolean startsWith(String prefix)

.
I define it as a method that is overloaded.

A method reference using a lambda parameter is intended to work with a functional interface BiPredicate<String, String>

, while a method reference using a variable not declared in lambda parameters is intended to work with a functional interface Predicate<String>

.

  • Method with variable passed as target of method reference:

    String myVariable; myVariable::startWith

provides already the string on which the method reference should be applied. Here: myVariable

.
Thus, only the prefix parameter should be passed to the lambda.
So it Predicate<String>

fits.



  • A way to use the first argument of a lambda parameter as the target of a method reference:

    String::startsWith

does not provide the string on which the method reference should be applied.
Thus, both the string on which the method should be called and the prefix parameters must be passed to the lambda.
So it BiPredicate<String, String>

fits.

Here's some sample code to illustrate:

public static void main(String[] args) {

    // using method reference with lambda parameter
    myMethodWithBiPredicate((s, prefix) -> s.startsWith(prefix), "mystring", "my");
    myMethodWithBiPredicate(String::startsWith, "mystring", "my");

    // using method reference with variable not in lambda parameter
    String stringNotInLambdaParams = "stringNotInParam";
    Predicate<String> functionPredicate = stringNotInLambdaParams::startsWith;

    System.out.print("myMethodWithBiPredicate with string "
            + "(included in the method reference)=" 
            + stringNotInLambdaParams 
            + " and prefix= string | Result = ");

    myMethodWithPredicate(functionPredicate, "string");

}

public static void myMethodWithBiPredicate(BiPredicate<String, String> function,
         String string,
         String prefix) {

    System.out.println("myMethodWithBiPredicate with string=" 
            + string + " and prefix= " + prefix 
            + " | Result = " + function.test(string, prefix));
}

public static void myMethodWithPredicate(Predicate<String> function, String prefix) {
    System.out.println(function.test(prefix));
}

      

which produces this output:

myMethodWithBiPredicate with string = mystring and prefix = my | result = true

myMethodWithBiPredicate with string = mystring and prefix = my | result = true

myMethodWithPredicate with string (included in reference method) = stringNotInParam and prefix = string | Result = true

+6


source







All Articles