Java Reference
In-Depth Information
List<Car> cars = Collections.emptyList();
Before Java 8, this inference mechanism based on the context (that is, target typing) was limited.
For example, the following wasn't possible:
static void cleanCars(List<Car> cars) {
}
cleanCars(Collections.emptyList());
You'd get the following error:
cleanCars (java.util.List<Car>)cannot be applied to (java.util.List<java.lang.Object>)
To fix it you'd have to provide an explicit type argument like the one we showed previously.
In Java 8 the target type includes arguments to a method, so you don't need to provide an
explicit generic argument:
List<Car> cleanCars = dirtyCars.stream()
.filter(Car::isClean)
.collect(Collectors.toList());
In this code, it's exactly this enhancement that lets you write Collectors.toList() instead of
Collectors.<Car>toList().
 
Search WWH ::




Custom Search