June 2017
Intermediate to advanced
400 pages
8h 44m
English
Lambdas can also implement single abstract methods that accept arguments and return an item. For instance, RxJava 2.0 (as well as Java 8) has a Function<T,R> type that accepts a T type and returns an R type. For instance, you can declare a Function<String,Integer>, whose apply() method will accept a String and return an Integer. Here, we implement apply() by returning the string's length in an anonymous class, as shown here:
import java.util.function.Function;public class Launcher { public static void main(String[] args) { Function<String,Integer> lengthMapper = new Function<String,Integer>() { @Override public Integer apply(String s) { return s.length(); } }; Integer length = lengthMapper.apply("Alpha"); System ...
Read now
Unlock full access