June 2017
Intermediate to advanced
400 pages
8h 44m
English
Lambdas can also implement methods that return items. For instance, the Supplier class introduced in Java 8 (and originally introduced in Google Guava) has an abstract get() method that returns a T item for a given Supplier<T>. If we have a Supplier<List<String>> whose get() returns List<String>, we can implement it using an old-fashioned anonymous class:
import java.util.ArrayList;import java.util.List;import java.util.function.Supplier;public class Launcher { public static void main(String[] args) { Supplier<List<String>> listGenerator = newSupplier<List<String>>() { @Override public List<String> get() { return new ArrayList<>(); } }; List<String> myList = listGenerator.get(); }}
But we can also use a lambda, ...
Read now
Unlock full access