import org.springframework.beans.BeanUtils;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

public class CopyUtils {

public static <T, K> List copyList(List source, Class target) {

if (null == source || source.isEmpty()) {
return Collections.emptyList();
}
return source.stream().map(e -> copy(e, target)).collect(Collectors.toList());
}

public static T copy(Object source, Class target) {
try {
T newInstance = target.newInstance();
BeanUtils.copyProperties(source, newInstance);
return newInstance;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}

posted on 2025-10-24 12:01  黎明前的守护  阅读(6)  评论(0)    收藏  举报