问题描述
我想使用一个使用泛型参数的方法,并在类层次结构上返回泛型结果。
编辑:否 SupressWarnings (unchecked)答案允许: - )
下面是一个演示我的问题的示例代码:
import java.util。*;
public class GenericQuestion {
interface Function< F,R> {R apply(F data);}
static class Fruit {int id;字符串名称; Fruit(int id,String name){
this.id = id; this.name = name;}
}
静态类Apple延伸Fruit {
Apple(int id,String type){super(id,type); }
}
静态类Pear extends Fruit {
Pear(int id,String type){super(id,type); }
public static void main(String [] args){
List< Apple>苹果= Arrays.asList(
新苹果(1,绿色),新苹果(2,红)
);
列表< Pear> pears = Arrays.asList(
新梨(1,绿),新梨(2,红)
);
函数fruitID = new函数< Fruit,Integer>(){
public Integer apply(Fruit data){return data.id;}
};
地图< Integer,Apple> appleMap = mapValues(apples,fruitID);
Map< Integer,Pear> pearMap = mapValues(pear,fruitID);
}
public static< K,V>地图< K,V> mapValues(
List< V>值,Function< V,K>函数){
Map< K,V> map = new HashMap< K,V>(); (v v:values)
(
map.put(function.apply(v),v);
}
返回地图;
$ b 如何从这些调用中删除泛型异常: / p>
地图< Integer,Apple> appleMap = mapValues(apples,fruitID);
Map< Integer,Pear> pearMap = mapValues(pear,fruitID);
额外问题:如果我以这种方式声明fruitId函数,如何消除编译错误:
函数< Fruit,Integer> fruitID = new Function< Fruit,Integer>(){public Integer apply(Fruit data){return data.id;}};
当泛型处理层次结构时,我非常困惑。任何指向关于使用情况的好资源的指针都将不胜感激。
解决方案 小改动:
public static void main(final String [] args){
// ... snip
//更改nr 1:使用通用声明
final函数< Fruit,Integer> fruitID =
新功能< Fruit,Integer>(){
@Override
public Integer apply(final Fruit data){
return data.id;
}
};
// ...剪切
}
公共静态< K,V>地图< K,V> mapValues(final List< V>值,
//改变nr.2:使用< ;? super V>而不是< V>
final Function< super V,K>函数){
// ...剪切
}
仅供参考,请阅读:
I want to use a method using generic parameters and returning generic result on a class hierarchy.
edit: no SupressWarnings("unchecked") answer allowed :-)
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:
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.
解决方案 2 small changes:
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:
这篇关于Java泛型,如何在使用类层次结构时避免未检查的赋值警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!