Java BeanUtils copyProperties 复制Arraylist

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19312055/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 16:00:03  来源:igfitidea点击:

BeanUtils copyProperties to copy Arraylist

javacopyapache-commons-beanutils

提问by Monicka Akilan

I know that BeanUtils can copy a single object to other.

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

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.

提前致谢。

采纳答案by Evgeniy Dorofeev

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

回答by Masudul

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

BeanUtils.copyProperties,它只复制同名的属性。所以,在 ArrayList 的情况下,你不能这样做。

According to docs:

根据文档:

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

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

回答by Ashish

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.

反正试试吧。

回答by David Gonzalez

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

回答by codemonkey

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.

更进一步的方法是使 List 类型通用 - 这假设您要复制 Vectors。

回答by blueDexter

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

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