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

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.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值