11

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

是否可以复制数组列表。

例如:

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

如何做到这一点?

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

它不适合我。谁能帮帮我吗。

提前致谢。

4

8 回答 8

16

如果您的列表来源的数据和列表目的地为空,则解决方案是:

    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);
     }
于 2015-05-14T11:19:29.103 回答
8

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

for (int i = 0; i < fromBeanList.size(); i++) {
     BeanUtils.copyProperties(toBeanList.get(i), fromBeanList.get(i));
}
于 2013-10-11T07:08:08.583 回答
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 类型通用 - 这假设您要复制 Vectors。

于 2016-06-07T08:04:01.900 回答
1

你可以试试这样的

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

希望这可以帮助..

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

反正试试吧。

于 2013-10-11T07:10:47.710 回答
0

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

根据文档:

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

于 2013-10-11T07:03:58.687 回答
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);
    }
}
于 2020-02-04T15:23:31.553 回答
0

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

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

于 2020-07-30T12:23:13.243 回答
-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());
    
                    }
            }
于 2020-11-25T11:55:19.933 回答