<p>public static <T> List<Object> convertBeanToObjects(final T bean,
final List<String> fields) {
if (CollectionUtils.isEmpty(fields)) {
return Collections.emptyList();
}
return fields.stream()
.map(item -&gt; getObjectData(bean, item))
.collect(Collectors.toList());
}</p>
<pre><code>private static &lt;T&gt; Object getObjectData(final T bean, 
                                      final String field) {
    // 获得字段值,通过反射直接获得字段值
    final Object fieldValue = BeanUtil.getFieldValue(bean, field);
    // 如果字段值是集合类型,将集合中的元素添加到结果列表中,并跳出本次循环
    if (fieldValue instanceof Collection) {
        return ((Collection&lt;?&gt;) fieldValue).stream()
            .collect(Collectors.toList());
    }
    // 否则,将单个字段值添加到结果列表中
    return fieldValue;
}
</code></pre>
Java Bean 对象属性转换为 List<Object> 的高效代码优化

原文地址: https://www.cveoy.top/t/topic/hKK6 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录