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
大家讲道理2017-05-17 10:09:31
Apache Commons's BeanUtils
和 Spring 的 BeanUtils
都有提供 copyProperties
and Spring's
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 + '}';
}
}
}
ChartData
和 DataBean
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. 🎜过去多啦不再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());
迷茫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.
阿神2017-05-17 10:09:31
The original poster should learn Java type conversion. Under these conditions, forced transfer is not possible.