Monicka Akilan
Monicka Akilan

Reputation: 1549

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.

Upvotes: 13

Views: 52483

Answers (8)

pattern cutter
pattern cutter

Reputation: 1

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

Upvotes: -1

martin robin A
martin robin A

Reputation: 1

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

Upvotes: 0

blueDexter
blueDexter

Reputation: 1170

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

Upvotes: 0

codemonkey
codemonkey

Reputation: 121

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.

Upvotes: 2

David Gonzalez
David Gonzalez

Reputation: 771

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

Upvotes: 19

Ashish
Ashish

Reputation: 745

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.

Upvotes: 1

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 136142

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

Upvotes: 10

Masudul
Masudul

Reputation: 21981

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.

Upvotes: 0

Related Questions