How do method references work in RxJava?

Can someone explain to me why lambdas can be replaced with a method link here?

In RxJava, map() takes a parameter of type Func1<T, R> , whose comment indicates that it "represents a function with argument one ." This way, I fully understand why valueOf(Object) works here. But trim() does not accept any arguments at all .

So how does it work?

 Observable.just("") .map(s -> String.valueOf(s)) //lambdas .map(s -> s.trim()) // .map(String::valueOf) //method references .map(String::trim) // .subscribe(); 
+6
source share
1 answer

I did not play with RX in java, but keep in mind that String::valueOf is a static (aka unbound) function, and String::trim is a non-static (aka bound) function that has an indirect this argument. Thus, in fact, both functions take one argument. In Java, this is not as noticeable as in Python for example.

+4
source

Source: https://habr.com/ru/post/1012233/


All Articles