Question

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.

Was it helpful?

Solution 2

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

OTHER TIPS

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

What you can do is to write your own generic copy class.

class CopyVector<S, T> {
    private Class<T> targetType;
    CopyVector(Class<T> targetType) {
        this.targetType = targetType;
    }
    Vector<T> copy(Vector<S> src) {
        Vector<T> target = new Vector<T>();
        for ( S s : src ) {
            T t = BeanUtils.instantiateClass(targetType);
            BeanUtils.copyProperties(s, t);
            target.add(t);
        }
        return target;
    }
}

A step further would also be to make the List type generic - this assumes you want to copy Vectors.

you can try something like this

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

Hope this helps..

Oops it is already explained by someone now..

anyways try it.

BeanUtils.copyProperties, It only copy the property of same name. So, In case of ArrayList you can't do that.

According to docs:

Copy property values from the origin bean to the destination bean for all cases where the property names are the same.

In spring BeanUtils.copyProperties, arguments are just opposite than apache commons lib

for(FromBean fromBean: fromBeanList) {
    if(fromBean != null) {
        ToBean toBean = new ToBean();
        org.springframework.beans.BeanUtils.copyProperties(fromBean, toBean);
        toBeanList.add(toBean);
    }
}

public List<"ToBean"> getAll() {
create an empty list of the Target folder

List<'ToBean>' toBeanList = new ArrayList<'ToBean'>();
Create a empty object of Target folder
ToBean toBean = new ToBean();
List<'FromBean > fromBeanList = beanRepository.findAll();
//Iterate Src Bean
for(fromBean : fromBeanList)
{
BeanUtils.copyProperties(fromBean , toBean );
toBeanList .add(toBean );
}
return toBeanList ; }

What i used just now :

     public static List<?\> copyPropertiesArray(List<?\> source, List<?\> destination,Class<?\> destinationClass) throws CopyPropertiesException {
                
                  try {
                    //the destination size must be the same as the source size and also it must be typed correctly (thus the need for the 3rd argument)         
                     destination=Collections.nCopies(source.size(),destinationClass.newInstance()); //initialize the desination list to same size as the source         
                       for (int i = 0; i < source.size(); i++) {                
                          BeanUtils.copyProperties(destination.get(i), source.get(i));
                       }

                       return destination;

                    } catch (IllegalAccessException | InvocationTargetException | InstantiationException e) {
                    
                       throw new Exception(e.getMessage());
    
                    }
            }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top