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();
trim
,Type::instanceMethod
is equivalent to(Type t) -> t.instanceMethod()
. (More generally,Type::instanceMethod
is(Type t, extraArgs) -> t.instanceMethod(extraArgs)
.) – Parthia