javacopyapache-commons-beanutils

BeanUtils copyProperties to copy Arraylist


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(fromBean, toBean);

How to achieve this?

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

It's not working for me. Can anyone please help me?

Thanks in advance.


Solution

  • If you have two lists of equals size then you can do the following

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