相关疑难解决方法(0)

为什么Function.identity()会破坏类型依赖,但t - > t不会?

Java 8 lambdas,Function.identity()或t-> t中找到的答案似乎暗示Function.identity()几乎总是等同于t -> t.然而,在测试用例看出下面,取代t -> tFunction.identity()编译错误的结果.这是为什么?

public class Testcase {

    public static <T, A, R, K, V> Collector<T, A, R> comparatorOrdering(
            Function<? super T, ? extends K> keyMapper,
            Function<? super T, ? extends V> valueMapper,
            Comparator<? super K> keyComparator,
            Comparator<? super V> valueComparator) {
        return null;
    }

    public static void main(String[] args) {    
        Map<Integer, String> case1 = Stream.of(1, 2, 3).
                collect(comparatorOrdering(t -> t, t -> String.valueOf(t),
                        Comparator.naturalOrder(), Comparator.naturalOrder())); …
Run Code Online (Sandbox Code Playgroud)

java generics reification

9
推荐指数
1
解决办法
1065
查看次数

标签 统计

generics ×1

java ×1

reification ×1