How to convert List<Object> into List<MyClass>?
Solution 1
Did you try to double-cast it like in answers to How to cast List<Object> to List<MyClass> question?
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
.
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? Is it going from your own code (code which you can modify) or from another library/code which you cannot modify?
Solution 2
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) - collects the resulting items into a new list
Note: I've tested this and it works.
Comments
-
S. Moreno almost 2 years
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.
My List of objects is
SalvaguardasAGR
and i want to convert this objects toList<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:
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;
too but still not working.What im doing wrong? how i can convert a List of objects into a List of AGRSalvaguardasInforme?
Thank you in advance.
-
Pracede over 9 yearsDo you having an exception ? If yes post stacktrace please
-
S. Moreno over 9 yearsAdded the stacktrace
-
Kalpesh Soni over 9 yearsgive code for mostrarInformeActivosAGR
-
user3062946 over 9 yearsThe current problem is that he's trying to create a new List with a List as a constructor parameter, the ArrayList constructor does not support handing in a List.
-
-
Kalpesh Soni over 9 yearsthis kind of casting does not work, e.g. List<Object> is not List<String>
-
Kalpesh Soni over 9 yearsyes, and to add - if possible do not use List<Object> create an interface for getters use List<MyInterface>
-
user3062946 over 9 yearsThat is true, but the implication he seems to be making is that the Objects in his original list ARE instances of 'AGRSalvaguardasInforme', and he just wanted a way to cast the list into the correct type.
-
ajb over 9 yearsI believe you're wrong about
ClassCastException
. The code you write does not check each element of the list to make sure it's the right type--I just tested it. That means doing this could possibly create aList<DesiredType>
that has objects that aren'tDesiredType
in it, defeating Java's whole type system.ClassCastExceptions
could come later, at totally unexpected places. -
Yuriy Nakonechnyy over 9 yearsLooks totally cool in
Java 8
- I thought about posting similar answer usingGuava
, but skipped that because of ugly anonymous classes :) -
S. Moreno over 9 years@Yura is from my own code. In this line is where returns a
Vector<E>
(and i cast, but i dont know why ignore that). and the error when i try to convert my objects list into myclass list:java.lang.ClassCastException: java.util.Vector cannot be cast to procop.agr.util.AGRSalvaguardasInforme
-
Yuriy Nakonechnyy over 9 years@S.Moreno could you please add code snippet producing
java.util.Vector
instance which you try to cast to List<DesiredType>? I'm sure there's decent typesafe solution to your problem :)