BeanUtils copyProperties复制Arraylist

Mon*_*lan 6 java copy apache-commons-beanutils

我知道BeanUtils可以将单个对象复制到其他对象.

是否可以复制一个arraylist.

例如:

 FromBean fromBean = new FromBean("fromBean", "fromBeanAProp", "fromBeanBProp");
 ToBean toBean = new ToBean("toBean", "toBeanBProp", "toBeanCProp");
 BeanUtils.copyProperties(toBean, fromBean);
Run Code Online (Sandbox Code Playgroud)

怎么做到这一点?

List<FromBean > fromBeanList = new ArrayList<FromBean >();  
List<ToBean > toBeanList = new ArrayList<ToBean >();  
BeanUtils.copyProperties(toBeanList , fromBeanList );
Run Code Online (Sandbox Code Playgroud)

它不适合我.谁能帮帮我吗.

提前致谢.

小智 12

如果您的数据列表原点和列表目标为空,则解决方案是:

    List<Object> listOrigin (with data)
    List<Object> listDestination= new ArrayList<Object>(); 

     for (Object source: listOrigin ) {
        Object target= new Object();
        BeanUtils.copyProperties(source , target);
        listDestination.add(target);
     }
Run Code Online (Sandbox Code Playgroud)

  • 这实际上是正确的实现,而不是公认的答案。! (2认同)

Evg*_*eev 5

如果您有两个等于大小的列表,那么您可以执行以下操作

for (int i = 0; i < fromBeanList.size(); i++) {
     BeanUtils.copyProperties(toBeanList.get(i), fromBeanList.get(i));
}
Run Code Online (Sandbox Code Playgroud)