Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从对象强制转换为列表的类型化实例的通用方法_Java_List_Generics_Casting - Fatal编程技术网

Java 从对象强制转换为列表的类型化实例的通用方法

Java 从对象强制转换为列表的类型化实例的通用方法,java,list,generics,casting,Java,List,Generics,Casting,假设有一个方法返回一个对象,但我们知道它是某种类型的列表(例如链接列表或数组列表): 直接使用其返回值 List<Long> result = (List<Long>)dialog.open(); 现在我可以这样使用它: List<Long> safeResult = this.doSafeCast(dialog.open(), Long.class); List<Long> safeResult = this.doSafeCast(dialo

假设有一个方法返回一个
对象
,但我们知道它是某种类型的
列表
(例如
链接列表
数组列表
):

直接使用其返回值

List<Long> result = (List<Long>)dialog.open();
现在我可以这样使用它:

List<Long> safeResult = this.doSafeCast(dialog.open(), Long.class);
List<Long> safeResult = this.doSafeCast(dialog.open(), Long.class, ArrayList.class);

List<String> anotherSafeResult = this.doSafeCast(anotherDialog.open(), String.class, LinkedList.class);

我知道这一切似乎过于复杂,如果我确信对话框总是返回所需类型,我可以将
@SuppressWarnings(“unchecked”)
添加到我的初始不安全强制转换中。但是我想到了一个通用的方法来做这样的安全转换,现在我想知道,这是否可行。因此,请不要建议其他解决方案,我只想在这里进一步了解泛型的使用。:-)谢谢。

是的,你可以。通过简单地为方法引入另一个类型参数并将其上界到
列表

private <T, L extends List<T>> List<T> doSafeCast(Object listObject, 
                                                  Class<T> type, 
                                                  Class<L> listClass) {
    List<T> result = listClass.newInstance();

    if (listObject instanceof List) {
        List<?> list = (List<?>) listObject;

        for (Object obj : list) {
            if (type.isInstance(obj)) {
                result.add(type.cast(obj));
            }
        }
    }
    return result;
}
private List doSafeCast(对象listObject,
类类型,
类别列表(类别){
列表结果=listClass.newInstance();
如果(列表对象实例列表){
List=(List)listObject;
对于(对象对象:列表){
if(类型isInstance(obj)){
结果添加(类型转换(obj));
}
}
}
返回结果;
}

请注意,您必须处理一些可能的异常。

Uuuh,我就差一点了。:-)我也尝试了同样的方法,但是使用了
L实现了List
。这使我获得了类型安全性:当我调用
doSafeCast()-实际上,这是你能做的最好的了,因为在运行时,你不能为你想要实例化的类指定泛型参数。好吧,如果我再次收到类型安全警告,那么这使得整个想法毫无用处。
List<Long> safeResult = this.doSafeCast(dialog.open(), Long.class);
List<Long> safeResult = this.doSafeCast(dialog.open(), Long.class, ArrayList.class);

List<String> anotherSafeResult = this.doSafeCast(anotherDialog.open(), String.class, LinkedList.class);
private <T, L extends List<T>> List<T> doSafeCast(Object listObject, 
                                                  Class<T> type, 
                                                  Class<L> listClass) {
    List<T> result = listClass.newInstance();

    if (listObject instanceof List) {
        List<?> list = (List<?>) listObject;

        for (Object obj : list) {
            if (type.isInstance(obj)) {
                result.add(type.cast(obj));
            }
        }
    }
    return result;
}