1.方法
import org.springframework.beans.BeanUtils; /** * 封装BeanUtils.copyProperties 数组转换 * @param resourceList 源数组 * @param target 目标对象 * @param <T> 目标对象类型 * @return */ public static <T> List<T> copyBeanList(List<?> resourceList, Class<T> target){ List<T> targetList = new LinkedList<>(); if (null==resourceList||resourceList.isEmpty()){ return targetList; } resourceList.forEach(e->{ T o = null; try { o = target.newInstance(); } catch (InstantiationException | IllegalAccessException ex) { ex.printStackTrace(); } BeanUtils.copyProperties(e,o); objects.add(o); }); return objects; }2.用法
List<A> listA=new ArrayList<>();
List<B> listB=copyBeanList(listA,B.class);