BeanUtils copyProperties 复制Arraylist

问题描述 投票:0回答:8

我知道 BeanUtils 可以将单个对象复制到其他对象。

是否可以复制数组列表。

例如:

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

如何实现这一点?

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

这对我不起作用。有人可以帮我吗?

提前致谢。

java copy apache-commons-beanutils
8个回答
18
投票

如果您的列表源包含数据且列表目标为空,则解决方案是:

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

9
投票

如果您有两个大小相等的列表,则可以执行以下操作

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

2
投票

你能做的就是编写你自己的通用复制类。

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

更进一步,还可以使 List 类型变得通用 - 这假设您要复制向量。


1
投票

你可以尝试这样的事情

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

希望这有帮助..

哎呀,现在已经有人解释了..

无论如何都要尝试一下。


0
投票

BeanUtils.copyProperties,仅复制同名属性。所以,对于 ArrayList 你不能这样做。

根据文档:

将属性值从源 bean 复制到目标 bean 属性名称相同的所有情况。


0
投票

在 spring BeanUtils.copyProperties 中,参数与 apache commons lib 正好相反

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

0
投票

公共列表<"ToBean"> getAll() {
创建目标文件夹的空列表

List<'ToBean>' toBeanList = new ArrayList<'ToBean'>();
创建Target文件夹的空对象
ToBean toBean = new ToBean();
List<'FromBean > fromBeanList = beanRepository.findAll();
//迭代Src Bean
for(fromBean:fromBeanList)
{
BeanUtils.copyProperties(fromBean, toBean);
toBeanList .add(toBean );
}
返回BeanList; }


-1
投票

我刚才用的:

     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());
    
                    }
            }
© www.soinside.com 2019 - 2024. All rights reserved.