![](/img/trans.png)
[英]Cannot cast from ArrayList<Object> to ArrayList<MyObject>
[英]Is it a good way to cast ArrayList<Object> to ArrayList<MyObject>?
基于这个Spring教程: http : //www.roseindia.net/tutorial/spring/spring3/ioc/springlistproperty.html我遇到了问题。 我使用Spring框架创建对象列表,但是我想获取列表列表。 从ArrayList强制转换为ArrayList是不可能的,因此我使用了自己的静态方法来执行此操作。 我们有两个类:
学生:
public class Student {
private String name;
private String address;
//getters and setters
}
学院:
import java.util.List;
public class College {
private List<Object> list;
public List<Object> getList() {
return list;
}
public void setList(List<Object> list) {
this.list = list;
}
}
还有context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="Student" class="testing.Student">
<property name="name" value="Thomas"/>
<property name="address" value="London"/>
</bean>
<bean id="College" class="testing.College">
<property name="list">
<list>
<value>1</value>
<ref bean="Student"/>
<bean class="testing.Student">
<property name="name" value="John"/>
<property name="address" value="Manchester"/>
</bean>
</list>
</property>
</bean>
</beans>
这是我的主要方法:
public static void main(String[] args) {
BeanFactory beanFactory = new ClassPathXmlApplicationContext(
"context.xml");
College college = (College) beanFactory.getBean("College");
}
我想在这里做的是通过从包含对象列表的大学对象接收通用的Student ArrayList。 这是我的解决方案:
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.ArrayList;
public class MainTest {
//This is my casting static method:
public static ArrayList<Student> castListToStudent(College college) {
ArrayList<Student> casted = new ArrayList<Student>();
Student s = null;
for (int i = 0; i < college.getList().size(); i++) {
if (college.getList().get(i) instanceof Student) {
s = (Student) college.getList().get(i);
casted.add(s);
}
}
return casted;
}
public static void main(String[] args) {
BeanFactory beanFactory = new ClassPathXmlApplicationContext(
"context.xml");
College college = (College) beanFactory.getBean("College");
ArrayList<Student> list = castListToStudent(college);
for (Student s : list) {
System.out.println(s);
}
}
}
看起来正在工作,但问题是-将清单安全地投射到另一个清单上,这是最好的方法吗?
使用番石榴:
List<Object> objects = Lists.newArrayList();
objects.add("A");
objects.add("B");
List<String> strings = FluentIterable.from(objects).filter(String.class).toList();
如果需要可变列表( ArrayList
),此示例将返回ImmutableList
:
List<String> strings = Lists.newArrayList(Iterables.filter(objects, String.class));
objects
中不是String
任何元素(在我的示例中)将被忽略。 这是一个完全类型安全的解决方案,不需要任何自写方法。
使用中间通配符进行转换可以完成转换工作。
List<Student> casted = (List<Student>)(List<?>) college.getList();
这更紧凑。 但是,您的方法更安全,因为这会导致未经检查的强制转换警告。
您的方法是最好的方法。
证明 : List<Object>
可以具有扩展Object的对象。 因此,如果您想安全地进行转换,则应检查List中的每个对象都是Student的实例。 因此,您必须遍历所有列表。 因此,就性能而言,没有比遍历整个列表更好的了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.