简体   繁体   English

如何转换清单 <Object> 进入清单 <MyClass> ?

[英]How to convert List<Object> into List<MyClass>?

in Java i have one List of Objects from my database and i want to convert this objects into my class objects but i cant find the solution. 在Java中,我有一个来自数据库的对象列表,我想将此对象转换为类对象,但是找不到解决方案。

My List of objects is SalvaguardasAGR and i want to convert this objects to List<AGRSalvaguardasInforme> . 我的对象列表为SalvaguardasAGR ,我想将此对象转换为List<AGRSalvaguardasInforme>

I tried with 我尝试过

List<AGRSalvaguardasInforme> InformeFinal = new ArrayList<AGRSalvaguardasInforme>(SalvaguardasAGR);

as i see in How to cast List<Object> to List<MyClass> and How to Convert List<String> to List<Object> but throw this exception: 正如我在如何将List <Object>强制 转换 为List <MyClass>以及如何将List <String>转换为List <Object>中看到的那样但抛出此异常:

    25-sep-2014 17:40:47 org.apache.catalina.core.StandardWrapperValve invoke
GRAVE: El Servlet.service() para el servlet [procop2front] en el contexto con ruta [/CDNMS] lanzó la excepción [java.lang.Error: Unresolved compilation problem: 
    The constructor ArrayList<AGRSalvaguardasInforme>(List<Object>) is undefined
] con causa raíz
java.lang.Error: Unresolved compilation problem: 
        The constructor ArrayList<AGRSalvaguardasInforme>(List<Object>) is undefined at com.dominion.procop.agr.struts.actions.AGRInformes.mostrarInformeActivosAGR(AGRInformes.java:1140)

I tried a simple List<AGRSalvaguardasInforme> InformeFinal = (AGRSalvaguardasInforme)SalvaguardasAGR; 我尝试了一个简单的List<AGRSalvaguardasInforme> InformeFinal = (AGRSalvaguardasInforme)SalvaguardasAGR; too but still not working. 也是,但仍然无法正常工作。

What im doing wrong? 我在做什么错? how i can convert a List of objects into a List of AGRSalvaguardasInforme? 如何将对象列表转换为AGRSalvaguardasInforme列表?

Thank you in advance. 先感谢您。

Java 8: Java 8:

List<AGRSalvaguardasInforme> InformeFinal = SalvaguardasAGR.stream().map(x -> (AGRSalvaguardasInforme)x).collect(Collectors.toList());

This: 这个:

  • treats the elements of the list as a stream, in order 按顺序将列表的元素视为流
  • transforms each element of the stream by casting it (actually, this doesn't do any transformation at all, but it does check to make sure the object is in the right class and throws a ClassCastException if not) 通过强制转换来转换流的每个元素(实际上,这根本不进行任何转换,但是会检查以确保对象位于正确的类中,如果不正确,则抛出ClassCastException
  • collects the resulting items into a new list 将结果项收集到新列表中

Note: I've tested this and it works. 注意:我已经对此进行了测试,并且可以正常工作。

Did you try to double-cast it like in answers to How to cast List<Object> to List<MyClass> question? 您是否试图像如何将List <Object>强制转换为List <MyClass>问题的答案那样进行双播?

List<Object> list = getList();
List<DesiredType> castedList = (List<DesiredType>) (List) list;

Be careful though, because calls to castedList.get(i) or other methods may throw ClassCastException if it turns out that list contains element which is not of DesiredType . 不过要小心,因为如果事实证明该list包含不是DesiredType元素,则对castedList.get(i)或其他方法的调用可能会引发ClassCastException

That's why I would really recommend to try and get List<DesiredType> instance right-away - where do you get instance of List<Object> from? 这就是为什么我会真正建议尝试立即获取List<DesiredType>实例的原因 -从何处获取List<Object>实例? Is it going from your own code (code which you can modify) or from another library/code which you cannot modify? 是来自您自己的代码(可以修改的代码)还是来自另一个无法修改的库/代码?

The easiest and most logical way would just be to iterate through the list of Objects, and individually cast them to the 'AGRSalvaguardasInforme' type, and add them to the other list, eg: 最简单,最合乎逻辑的方法是遍历对象列表,并将其分别转换为“ AGRSalvaguardasInforme”类型,然后将其添加到其他列表中,例如:

 for (Object o : SalvaguardasAGR)
        InformeFinal.add((AGRSalvaguardasInforme) o);

You could also try doing the direct cast of the list like so: 您也可以尝试直接对列表进行转换,如下所示:

 List<AGRSalvaguardasInforme> InformeFinal = (List<AGRSalvaguardasInforme>)(Object) SalvaguardasAGR

Although that method will flag an unchecked cast warning. 尽管该方法将标记未经检查的强制转换警告。

If you absolutely know the objects are assignable to AGRSalvaguardasInforme, you'll need to cast the elements directly and add them to a list with the target type: 如果您完全知道对象可以分配给AGRSalvaguardasInforme,则需要直接转换元素并将它们添加到目标类型的列表中:

public static <T> List<T> cast(Collection<?> list) {
    List<T> valueList = new ArrayList<T>(list.size());

    for(Object o : list) {
        // throws casting exception
        valueList.add((T)o);  
    }

    return valueList;
}

You can call this method like this: 您可以这样调用此方法:

List<AGRSalvaguardasInformeme> list = cast(salvaguardasAGR);

If handled properly, you shouldn't ever need to cast a list of objects to a list of something else. 如果处理得当,则无需将对象列表转换为其他对象列表。 If you have a List of AGRSalvaguardasInformeme, why to create the properly typed list initially? 如果您有AGRSalvaguardasInformeme列表,为什么要首先创建正确键入的列表? I understand there are situations where you cant avoid this (like using a poorly written library). 我了解在某些情况下您无法避免这种情况(例如使用编写不佳的库)。

Here's the brute-force way: 这是蛮力方式:

// initialize new list with the same size as the old list
List<AGRSalvaguardasInforme> InformeFinal = new ArrayList<AGRSalvaguardasInforme>(SalvaguardasAGR.size());
// cast and insert the contents of the old list into the new list
for(Object obj : SalvaguardasAGR)
{
    AGRSalvaguardasInforme newObj = (AGRSalvaguardasInforme) obj;
    InformeFinal.add(newObj);
}

Note that you'll get a ClassCastException if the Object s aren't instances of AGRSalvaguardasInforme . 请注意,如果Object不是AGRSalvaguardasInforme实例, AGRSalvaguardasInforme获得ClassCastException

if SalvaguardasAGR is List of Object (instead of AGRSalvaguardasInforme) 如果SalvaguardasAGR是对象列表(而不是AGRSalvaguardasInforme)

you ll have to iterate and do this manually 您将不得不迭代并手动执行此操作

the links you posted go from List to List - specific to generic 您发布的链接从列表到列表-特定于通用

You are doing the reverse thing, generic to specific 您正在做相反的事情,针对特定情况

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

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