February 2020
Intermediate to advanced
412 pages
9h 36m
English
Lambda expressions can also implement the Function interface, which has a single abstract method that accepts arguments and returns an item. For instance, RxJava 2.0 (as well as Java 8) has a Function<T, R> interface that accepts a T type and returns an R type. This means that you can declare a Function<String, Integer>, whose apply() method accepts a String and returns an Integer. For example, we can implement apply() by returning the string's length in an anonymous class, as shown here:
import java.util.function.Function;public class A_12 { public static void main(String[] args) { Function<String,Integer> lengthMapper = new Function<String, Integer>() { @Override public Integer apply(String s) { return s.length() ...
Read now
Unlock full access