简体   繁体   English

使用泛型和参数化列表的问题

[英]Issue using Generics and Parameterized List

I created the following Test class: 我创建了以下测试类:

public class GenericTest {

    public static class A implements Serializable {

    }

    public static class B implements Serializable {

    }

    public static void main(String[] args) {
        // Error: Type mismatch: cannot convert from List<capture#1-of ? extends Serializable> to List<GenericTest.A>
        //List<A> aList = getInfo().get("A");
        //List<B> BList = getInfo().get("B");

        // Warning: Type safety: Unchecked cast from List<capture#1-of ? extends Serializable> to List<GenericTest.A>
        List<A> aList = (List<A>)getInfo().get("A");
        List<B> BList = (List<B>)getInfo().get("B");
    }

    public static Map<String, List<? extends Serializable>> getInfo() {
        Map<String, List<? extends Serializable>> infoMap = new HashMap<String, List<? extends Serializable>>();

        List<A> aList = new ArrayList<A>();
        List<B> bList = new ArrayList<B>();

        try {
            aList.add(new A());
            infoMap.put("A", aList);

            bList.add(new B());
            infoMap.put("B", bList);
        }
        catch(Exception e) {
            e.printStackTrace();
        }

        return infoMap;
    }    
}

Is there a better way to go about this to avoid casting and suppressing the unchecked warning? 是否有更好的方法来避免这种情况,避免投射和抑制未经检查的警告? I have been told using casting almost defeats the purpose of using Generics in the first place. 有人告诉我使用强制转换几乎打败了使用泛型的目的。 Is there a problem with this, or a "safer" way to go about doing it? 这有问题吗,或者是“安全”的方法呢?

you can try doing this. 您可以尝试执行此操作。 You still need to suppress warnings in the getList method, but if you only add lists using the addToMap method ,the compiler can correctly check if the added list is the same type of class that was used in the first parameter. 您仍然需要在getList方法中禁止显示警告,但是如果仅使用addToMap方法添加列表,则编译器可以正确检查添加的列表是否与第一个参数中使用的类类型相同。 Also google for super type tokens. 谷歌也为超级类型令牌。

public static void main(String[] args) {
    List<A> aList = new ArrayList<A>();
    aList.add(new A());
    List<B> bList = new ArrayList<B>();
    bList.add(new B());
    addToMap(A.class,aList);
    addToMap(B.class,bList);        

    List<A> aListFromMap = getList(A.class);
    List<B> bListFromMap = getList(B.class);
}



private static Map<Class<?>,Object> infoMap = new HashMap<Class<?>,Object>();



public static <T extends Serializable> void addToMap(Class<T> key, List<T> value) {
    infoMap.put(key,value);
}


public static <T extends Serializable> List<T> getList(Class<T> key) {
    return (List<T>)(infoMap.get(key));
} 

No, there is no way to help this situation. 不,没有办法解决这种情况。 You have a single map with two kinds of values (this is called a heterogeneous map) and the type system cannot express that. 您只有一个包含两种值的映射(这称为异类映射),类型系统无法表达该值。 You must downcast without type safety. 您必须在没有类型安全的情况下沮丧。 Either that, or completely redesign to keep these two kinds of objects in two separate structures. 要么完全重新设计,要么将这两种对象保留在两个单独的结构中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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