Home  >  Q&A  >  body text

java - Convert list<bean> to a list of another bean.

public static class DataBean {
    private int value;
    private BigDecimal name;}
public class ChartData {
    private Integer time;
    private BigDecimal result;}

I need operations similar to the following,

List<ChartData> data = getdata();
List<SeriesBean.DataBean> yValue = data.stream().map(item -> (SeriesBean.DataBean) item);

Error reporting non-convertible type, DataBean is an internal static class.
There is reinterpret_cast in C that can be forced to be transferred, and Java should have the corresponding method

PHP中文网PHP中文网2711 days ago996

reply all(4)I'll reply

  • 大家讲道理

    大家讲道理2017-05-17 10:09:31

    Apache Commons's BeanUtilsSpringBeanUtils 都有提供 copyProperties and Spring's

    both provide the copyProperties method, which is to assign the value of the properties of one object to another object, but only if the property types of the two objects are And the

    nameBeanUtils is the same.

    For example using Apache Commons :

    import java.math.BigDecimal;
    import org.apache.commons.beanutils.BeanUtils;
    
    public class TestBeanUtils {
    
        public static void main(String[] args) throws Exception {
    
            ChartData src = new ChartData(1, BigDecimal.valueOf(123));
            DataBean dest = new DataBean();
    
            BeanUtils.copyProperties(dest, src);
    
            System.out.println(src);
            System.out.println(dest);
        }
    
        public static class DataBean {
    
            private int time;
            private BigDecimal result;
    
            public int getTime() {
                return time;
            }
    
            public void setTime(int time) {
                this.time = time;
            }
    
            public BigDecimal getResult() {
                return result;
            }
    
            public void setResult(BigDecimal result) {
                this.result = result;
            }
    
            @Override
            public String toString() {
                return "DataBean{" + "time=" + time + ", result=" + result + '}';
            }
    
        }
    
        public static class ChartData {
    
            private Integer time;
            private BigDecimal result;
    
            public ChartData(Integer time, BigDecimal result) {
                this.time = time;
                this.result = result;
            }
    
            public Integer getTime() {
                return time;
            }
    
            public BigDecimal getResult() {
                return result;
            }
    
            public void setTime(Integer time) {
                this.time = time;
            }
    
            public void setResult(BigDecimal result) {
                this.result = result;
            }
    
            @Override
            public String toString() {
                return "ChartData{" + "time=" + time + ", result=" + result + '}';
            }
    
        }
    
    }
    


    ChartDataDataBean

    So if the attribute names of

    are the same, your code can be written like this (there is no need to write setter methods for each attribute): 🎜
    List<ChartData> data = getdata();
    List<DataBean> yValue = new ArrayList<>(data.size());
    for (ChartData item : data) {
        DataBean bean = new DataBean();
        BeanUtils.copyProperties(bean, item);
        yValue.add(bean);
    }
    🎜Of course, one thing to note is that this is implemented using reflection, which is less efficient than writing the setter method directly. 🎜

    reply
    0
  • 过去多啦不再A梦

    过去多啦不再A梦2017-05-17 10:09:31

    List<DataBean> yValue = data.stream().map(item -> {
        DataBean bean = new DataBean();
        bean.setName(item.getResult());
        bean.setValue(item.getTime());
        return bean;
    }).collect(Collectors.toList());

    reply
    0
  • 迷茫

    迷茫2017-05-17 10:09:31

    Forced transfer can only be done from parent class to child class. Just click on each field and set it.

    reply
    0
  • 阿神

    阿神2017-05-17 10:09:31

    The original poster should learn Java type conversion. Under these conditions, forced transfer is not possible.

    reply
    0
  • Cancelreply