码迷,mamicode.com
首页 > 其他好文 > 详细

How to cast List<Object> to List<MyClass> Object集合转换成实体集合

时间:2017-06-30 09:39:24      阅读:248      评论:0      收藏:0      [点我收藏+]

标签:com   get   gen   home   types   地址   size   集合   span   

List<Object> list = getList();

return (List<Customer>) list;

Compiler says: cannot cast List<Object> to List<Customer>
不能将Object集合强制转换成实体集合!
 

you can always cast any object to any type by up-casting it to Object first. in your case:

(List<Customer>)(Object)list; 
将List<Object>强制转换成Object,然后再转换为实体集合!
地址:https://stackoverflow.com/questions/1917844/how-to-cast-listobject-to-listmyclass

you must be sure that at runtime the list contains nothing but Customer objects.

Critics say that such casting indicates something wrong with your code; you should be able to tweak your type declarations to avoid it. But Java generics is too complicated, and it is not perfect. Sometimes you just don‘t know if there is a pretty solution to satisfy the compiler, even though you know very well the runtime types and you know what you are trying to do is safe. In that case, just do the crude casting as needed, so you can leave work for home.

How to cast List<Object> to List<MyClass> Object集合转换成实体集合

标签:com   get   gen   home   types   地址   size   集合   span   

原文地址:http://www.cnblogs.com/SimonHu1993/p/7097218.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!