12

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.

Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
Monicka Akilan
  • 1,501
  • 5
  • 19
  • 42

8 Answers8

18

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);
     }
David Gonzalez
  • 751
  • 7
  • 6
9

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));
}
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
2

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.

abarisone
  • 3,707
  • 11
  • 35
  • 54
codemonkey
  • 121
  • 1
  • 2
1

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.

Ashish
  • 735
  • 1
  • 6
  • 15
0

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.

Masudul
  • 21,823
  • 5
  • 43
  • 58
0

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);
    }
}
blueDexter
  • 977
  • 11
  • 14
0

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

  • 1
    Welcome to Stack Overflow. I suggest you separate code and explanation into separate parts in your answer. Also format the code, refer to the [help page on Markdown](https://stackoverflow.com/editing-help) on how to do it. Possibly you also want to refer to [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer). – Ivo Mori Jul 30 '20 at 12:46
-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());
    
                    }
            }
Azzabi Haythem
  • 2,318
  • 7
  • 26
  • 32