简体   繁体   中英

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:

编译器错误

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;
    }
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM