Let's look at Function in detail:
Interface Function<T,R> {
default <V> Function<T,V> andThen(Function<? super R,? extends V> after){}
R apply(T t);
default <V> Function<V,R> compose(Function<? super V,? extends T> before){}
static <T> Function<T,T> identity();
}
note the R apply(T t); Applies this function to the given argument.
Function<Void, Integer> function = Void::?????;
Void voidInstance = null;
function.apply(voidInstance);
This doesn't make sense. You want to pass a Void so that the function of the Void is applied ?
A few illustrative examples of what compiles as a function
note that c->c.getMaxSpeed() and Car::getMaxSpeed are syntactically equivalent if the method is an instanceMethod. For non-static-methods the first argument is infered from the type who's method is used, and needs to be provided later (as the instance which the method will be executed on/applied on).
public class Car {
private int maxSpeed;
public Car(int maxSpeed) {
this.maxSpeed = maxSpeed;
}
public int getMaxSpeed() {
return this.maxSpeed;
}
public Void setMaxSpeed() {
this.maxSpeed = 12;
return null;
}
public static int intStaticFunction(Void v) {
return new Random().nextInt();
}
public static Void voidStaticFunction(Void v) {
return null;
}
public static void main(String[] args) {
final Car carX = new Car(155);
final Car carY = new Car(140);
final List<Car> cars = new ArrayList<>();
cars.add(carX);
cars.add(carY);
cars.sort(Comparator.comparing(Car::getMaxSpeed));
final Function<Car, Integer> function1 = c->c.getMaxSpeed();
final Function<Car, Integer> function2 = Car::getMaxSpeed;
final Function<Car, Void> function3 = Car::setMaxSpeed;
final Function<Void, Void> function4 = n->n;
final Function<Void, Integer> function5 = n->5;
final Function<Void, Integer> function6 = Car::intStaticFunction;
final Function<Void, Void> function7 = Car::voidStaticFunction;
final Function<Car, Integer> function8 = function1::apply;
final Function<Car, Integer> function9 = function2::apply;
System.out.println(function1.apply(carX));
System.out.println(function2.apply(carX));
System.out.println(function8.apply(carX));
System.out.println(function9.apply(carX));
System.out.println(function3.apply(carX));
System.out.println(function1.apply(carX));
System.out.println(function2.apply(carX));
System.out.println(function8.apply(carX));
System.out.println(function9.apply(carX));
System.out.println();
System.out.println(function4.apply(null));
System.out.println(function5.apply(null));
System.out.println(function6.apply(null));
System.out.println(function7.apply(null));
}
}
Comparator.comparingIntto avoid boxing.carX::getMaxSpeedwhich is very different thanCar::getMaxSpeed. Most answers talk about the first revision, not all. This is confusing: your edit invalidated part of the answers...Void, that is, expecting an instance ofjava.lang.Void, but you are unlikely to ever see something other thannullthere, so it’s not much useful. The point is,Voidis not a wrapper type forvoid, it’s only a placeholder forVoid.TYPEwhich holds theClassobject representingvoid.class.