How to convert List<Object> to List<Dog> in Java

Syed Hassan

How to convert List to List. I tried doing this by

List<Object> obj = (some initialization)
List<Dog> dog =(List<Dog>)obj;  

So this throws an error

ERROR: Cannot cast from List<Object> to List<Dog>

Where

 List<Object> 

I get from the Hibernate Query

FROM DOG 
wero

The cast works if you use a List<?> instead:

List<?> list = (some initialization);
List<Dog> dog = (List<Dog>)list;

If you got a List<Object> you still can cast it to a List<Dog>

List<Object> list = (some initialization);
List<Dog> dog = (List)list;

Of course this should only be done when you are sure that the list really contains Dog objects (e.g. as result value from your HQL query FROM Dog).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related