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

Java泛型,在使用类层次结构时如何避免未检查的赋值警告?,java,generics,compiler-warnings,Java,Generics,Compiler Warnings,我想使用一个使用泛型参数并在类层次结构上返回泛型结果的方法 编辑:否超级警告(“未选中”)允许回答:-) 下面是一个示例代码,说明了我的问题: 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)

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

编辑:超级警告(“未选中”)允许回答:-)

下面是一个示例代码,说明了我的问题:

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;
    }
}
import java.util.*;
公共类泛型问题{
接口函数{R apply(F data);}
静态类水果{int-id;字符串名称;水果(int-id,字符串名称){
this.id=id;this.name=name;}
}
静态类Apple扩展了{
Apple(int-id,String-type){super(id,type);}
}
静态类{
Pear(int-id,String-type){super(id,type);}
}
公共静态void main(字符串[]args){
List apples=Arrays.asList(
新苹果(1,“绿色”),新苹果(2,“红色”)
);
List pears=Arrays.asList(
新梨(1,“绿色”),新梨(2,“红色”)
);
函数fructId=新函数(){
公共整数apply(水果数据){return data.id;}
};
Map appleMap=mapValues(苹果,果味);
Map pearMap=mapValues(梨,果状体);
}
公共静态映射值(
列表值(函数){
Map Map=newhashmap();
对于(V:值){
map.put(函数.应用(v),v);
}
返回图;
}
}
如何从这些调用中删除常规异常:

Map<Integer, Apple> appleMap = mapValues(apples, fruitID);
Map<Integer, Pear> pearMap = mapValues(pears, fruitID);
Map appleMap=mapValues(苹果,果肉ID);
Map pearMap=mapValues(梨,果状体);
额外的问题:如果我以这种方式声明FROUTID函数,如何删除编译错误:

Function<Fruit, Integer> fruitID = new Function<Fruit, Integer>() {public Integer apply(Fruit data) {return data.id;}};
Function fruitID=new Function(){public Integer apply(水果数据){return data.id;}};
在处理层次结构时,我对泛型非常困惑。任何关于和的使用的好资源的指针都将非常感谢。

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
}
publicstaticvoidmain(最终字符串[]args){
//…剪断
//更改1:使用泛型声明
最终功能果体=
新函数(){
@凌驾
应用公共整数(最终水果数据){
返回data.id;
}
};
//…剪断
}
公共静态映射值(最终列表值,
//第2项更改:使用