Search code examples
javagenericsfunctional-interface

java cast generic type without unchecked cast warning


I'm trying to understand how to use the Function<T,R> interface, defined in java libraries. Why passing an instance of DoubleIt to map works, but passing a constructor reference get the compiler error below, and is there any way to do that:

compiler error

import java.util.Arrays;
import java.util.function.Function;
import java.util.stream.Stream;

public class C {
    public static void main(String[] args) {
        final Stream<Integer> stream = Arrays.stream(new Integer[]{2, Integer.MAX_VALUE});

        DoubleIt<Integer, Long> doubleIt = new DoubleIt<>(); 
        stream.map(doubleIt)                     // OK      instance
                .forEach(System.out::println);

        stream.map(DoubleIt<Integer, Long>::new) // error   constructor reference
                .forEach(System.out::println);
    }
}

class DoubleIt<T extends Integer, R extends Long> implements Function<T, R> {

    @Override
    public R apply(T t) {
        @SuppressWarnings("unchecked")
        final R r = (R) Long.valueOf(t.intValue() * 2L);
        return r;
    }
}

Solution

  • The first one passes a DoubleIt instance, the other one passes a method to construct a DoubleIt instance, the two have very different signatures. Why would you expect them to be interchangeable?

    The first can consume an Integer and return a Long, the second one can consume nothing and return a DoubleIt. They do very different things and therefore you cannot use one in place of the other.