java list的深拷贝


一般的写法:

//不使用stream拷贝list
        List<WarehouseStockItemBean> dtoList = new ArrayList<>();
        for (WarehouseStockItemBean warehouseStockItem : warehouseStockItemBeanList) {
            WarehouseStockItemBean d = new WarehouseStockItemBean();
            BeanUtils.copyProperties(warehouseStockItem, d);
            dtoList.add(d);
        }

使用java8的stream流写法:

warehouseStockItemBeanLists = warehouseStockItemBeanList.stream()
                .map(e -> {
                    WarehouseStockItemBean d = new WarehouseStockItemBean();
                    BeanUtils.copyProperties(e, d);
                    return d;
                })
                .collect(Collectors.toList());

两者在实现User对象拷贝到UserDTO对象时都是用了spring中内置的BeanUtils(这类工具类很多。我这里用的是spring自带的)

实现原理都是遍历集合拷贝对象然后添加到新集合

 

转自:http://www.3kkg.com/1095

侵删


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM