BeanUtils copyProperties to copy Arraylist

Monicka Akilan picture Monicka Akilan · Oct 11, 2013 · Viewed 34.9k times · Source

I know that BeanUtils can copy a single object to other.

Is it possible to copy an arraylist.

For example:

 FromBean fromBean = new FromBean("fromBean", "fromBeanAProp", "fromBeanBProp");
 ToBean toBean = new ToBean("toBean", "toBeanBProp", "toBeanCProp");
 BeanUtils.copyProperties(toBean, fromBean);

How to achieve this?

List<FromBean > fromBeanList = new ArrayList<FromBean >();  
List<ToBean > toBeanList = new ArrayList<ToBean >();  
BeanUtils.copyProperties(toBeanList , fromBeanList );

Its not working for me. Can any one please help me.

Thanks in advance.

Answer

David Gonzalez picture David Gonzalez · May 14, 2015

If you have a list origin with data and list destination empty, the solution is:

    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);
     }