javalambdarx-javaretrolambda

How do method references in RxJava work?


Can someone explain me, how come both of the lambdas can be replaced with method references here?

In RxJava, map() takes a parameter of type Func1<T, R>, whose comment states that it "Represents a function with one argument". Thus I completely understand why valueOf(Object) works here. But trim() takes no arguments at all.

So how does this work exactly?

Observable.just("")
    .map(s -> String.valueOf(s))  //lambdas
    .map(s -> s.trim())           //

    .map(String::valueOf)         //method references
    .map(String::trim)            //
    .subscribe();

Solution

  • I didn't play with RX in java, but please note, that String::valueOf is a static (aka unbound) function, while String::trim is a non-static (aka bound) function that have indirect this argument. So, in fact, both function takes single argument. In Java it's not that visible as it is in Python for example.