java Java泛型,如何在使用类层次结构时避免未经检查的赋值警告?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5309922/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 10:33:02  来源:igfitidea点击:

Java Generics, how to avoid unchecked assignment warning when using class hierarchy?

javagenericscompiler-warnings

提问by Guillaume

I want to use a method using generic parameters and returning generic result on a class hierarchy.

我想使用一种使用泛型参数并在类层次结构上返回泛型结果的方法。

edit: noSupressWarnings("unchecked")answer allowed :-)

编辑:不允许SupressWarnings("unchecked")回答:-)

Here is a sample code illustrating my problem:

这是说明我的问题的示例代码:

import java.util.*;

public class GenericQuestion {

    interface Function<F, R> {R apply(F data);}
    static class Fruit {int id; String name; Fruit(int id, String name) {
        this.id = id; this.name = name;}
    }
    static class Apple extends Fruit { 
        Apple(int id, String type) { super(id, type); }
    }
    static class Pear extends Fruit { 
        Pear(int id, String type) { super(id, type); }
    }

    public static void main(String[] args) {

        List<Apple> apples = Arrays.asList(
                new Apple(1,"Green"), new Apple(2,"Red")
        );
        List<Pear> pears = Arrays.asList(
                new Pear(1,"Green"), new Pear(2,"Red")
        );

        Function fruitID = new Function<Fruit, Integer>() {
            public Integer apply(Fruit data) {return data.id;}
        };

        Map<Integer, Apple> appleMap = mapValues(apples, fruitID);
        Map<Integer, Pear> pearMap = mapValues(pears, fruitID);
    }

      public static <K,V> Map<K,V> mapValues(
              List<V> values, Function<V,K> function) {

        Map<K,V> map = new HashMap<K,V>();
        for (V v : values) {
            map.put(function.apply(v), v);
        }
        return map;
    }
}

How to remove the generic exception from these calls:

如何从这些调用中删除通用异常:

Map<Integer, Apple> appleMap = mapValues(apples, fruitID);
Map<Integer, Pear> pearMap = mapValues(pears, fruitID);

Bonus question: how to remove the compilation error if I declare the fruitId Function this way:

额外问题:如果我以这种方式声明 FruitId 函数,如何消除编译错误:

Function<Fruit, Integer> fruitID = new Function<Fruit, Integer>() {public Integer apply(Fruit data) {return data.id;}};

I'm very confused about generics when it is dealing with hierarchy. Any pointer to a good resource about the usage of and will be greatly appreciated.

在处理层次结构时,我对泛型感到非常困惑。任何指向有关 和 使用的良好资源的指针将不胜感激。

回答by Sean Patrick Floyd

2 small changes:

2个小改动:

public static void main(final String[] args){

    // ... snip

    // change nr 1: use a generic declaration
    final Function<Fruit, Integer> fruitID =
        new Function<Fruit, Integer>(){

            @Override
            public Integer apply(final Fruit data){
                return data.id;
            }
        };

    // ... snip
}

public static <K, V> Map<K, V> mapValues(final List<V> values,

    // change nr. 2: use <? super V> instead of <V>
    final Function<? super V, K> function){

    // ... snip
}


For reference, read this:

作为参考,请阅读以下内容:

The get-put principle

get-put原则