How to cast List<Object> to List<MyClass> Object集合转换成实体集合
2017-09-01 14:28
726 查看
List<Object> list = getList();
Compiler says: cannot cast
不能将Object集合强制转换成实体集合!
you can always cast any object to any type by up-casting it to Object first. in your case:
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.
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:
[b][b]([b]List[/b]<[b]Customer[/b]>)([b]Object[/b])[b]list[/b][/b][b];[/b] 将List<Object>强制转换成Object,然后再转换为实体集合! 地址:https://stackoverflow.com/questions/1917844/how-to-cast-listobject-to-listmyclass[/b][/code]
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.
相关文章推荐
- c# List<实体>转换List<object>集合
- C#将DataTable转换成单个T实体和List<T>集合
- ctypes.ArgumentError: argument 1: <class 'TypeError'>: Don't know how to convert parameter 1
- Ilist<object>转换成I<实体> 如何转换
- object[] 转换成指定List<T>
- Type safety: Unchecked cast from List to List<Book>
- How to sort a generic List<T>
- struts2的<s:iterator>标签 遇到的问题:java.lang.ClassCastException:cannot be cast to java.lang.String
- Java集合转换【List<-->数组、List<-->Set、数组<-->Set、Map-->Set、Map-->List】
- Downcast from'[NSObject:AnyObject]?'to'Dictionary<NSObject,AnyObject>'only unwraps
- 使用Map把sql返回的list<Object>转成需要的实体对象
- List<Object> 转为 List<MyClass>
- 将DataTable转换成List<T>泛型集合助手类
- 实体类集合(List<Object>)根据实体类的一个或者多个属性对集合进行排序-Collections.sort
- Hql的select 语句返回Ilist<object[]>类型,如何转换为映射类型
- C#读取数据库返回泛型集合 把DataSet类型转换为List<T>泛型集合
- dynamic_cast<const ObjectList&>(msg);
- 泛型中字符串转集合,(T)object,typeOf(T),LIst<T> xxx<T>();c# stringtolist
- hibernate使用本地sql语句查询,解决List<Object[]>转换成List<实体>