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
Apache Commons's
both provide theBeanUtils
和 Spring 的BeanUtils
都有提供copyProperties
and Spring'scopyProperties
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 thename
BeanUtils
is the same.For example using Apache Commons :
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): 🎜 🎜Of course, one thing to note is that this is implemented using reflection, which is less efficient than writing the setter method directly. 🎜Forced transfer can only be done from parent class to child class. Just click on each field and set it.
The original poster should learn Java type conversion. Under these conditions, forced transfer is not possible.