使用BeanUtils的copyProperties方法来复制ArrayList。

12
我知道BeanUtils可以将一个对象复制到另一个对象。
那么,是否可以复制一个ArrayList呢?
例如:
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);

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

提前感谢。

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

2
这个实现才是正确的,而不是被接受的答案! - Md Faisal

9
如果您有两个相等大小的列表,则可以执行以下操作:
for (int i = 0; i < fromBeanList.size(); i++) {
     BeanUtils.copyProperties(toBeanList.get(i), fromBeanList.get(i));
}

现在我是这样做的,但我想知道如何一次完全复制,谢谢。 - Monicka Akilan
不要忘记,copyProperties 只会复制具有公共 set 方法的属性,它不使用反射。 - Pedro Bacchini

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

public List<"ToBean"> getAll() {
创建一个空的目标文件夹列表

List<'ToBean>' toBeanList = new ArrayList<'ToBean'>();
创建一个空的目标文件夹对象
ToBean toBean = new ToBean();
List<'FromBean > fromBeanList = beanRepository.findAll();
//遍历源Bean
for(fromBean : fromBeanList)
{
BeanUtils.copyProperties(fromBean , toBean );
toBeanList .add(toBean );
}
return toBeanList ; }


1
欢迎来到Stack Overflow。我建议你将代码和解释分开放在答案的不同部分中。同时,格式化代码时请参考Markdown的帮助页面。如果可能的话,你还可以参考如何编写好的答案? - Ivo Mori

0
在 Spring 的 BeanUtils.copyProperties 中,参数和 Apache Commons 库中的相反。
for(FromBean fromBean: fromBeanList) {
    if(fromBean != null) {
        ToBean toBean = new ToBean();
        org.springframework.beans.BeanUtils.copyProperties(fromBean, toBean);
        toBeanList.add(toBean);
    }
}

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接