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
Iterate over the List<Object> and cast every item explicitely
List<Dog> dog = new ArrayList<Dog>;
for (Object o : obj)
dog.add((Dog) o);
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With