一、场景中,两个List《A》,List《B》,A 包含 B,或者A,B类中有相同的东西

二、代码示例

2.1 Customer对象

package tonels.mbdemo3.entity;

import lombok.Data;
import lombok.experimental.Accessors;

import java.math.BigDecimal;

@Data
@Accessors(chain = true)
public class Customers {

    private Integer customernumber;

    private String customername;

    private String contactlastname;

    private String contactfirstname;

    private String phone;

    private String addressline1;

    private String addressline2;

    private String city;

    private String state;

    private String postalcode;

    private String country;

    private Integer salesrepemployeenumber;

    private BigDecimal creditlimit;


}

2.2 CustomerVo对象

package tonels.mbdemo3.vo;

import lombok.Data;

@Data
public class CustoVo {

    private String customernumber;

    private String customername;

    private String contactlastname;

    private String contactfirstname;
}

三、处理过程

3.1

 List<Customers> all = customersService.findAll2();
 List<CustoVo> vos= all.stream().map(e -> JSONUtil.toBean(JSONUtil.toJsonStr(e), CustoVo.class)).collect(Collectors.toList());

转换前 all

 [{
"customernumber": 131,
"customername": "Land of Toys Inc.",
"contactlastname": "Lee",
"contactfirstname": "Kwai",
"phone": null,
"addressline1": null,
"addressline2": null,
"city": null,
"state": null,
"postalcode": null,
"country": null,
"salesrepemployeenumber": null,
"creditlimit": null
},
{
"customernumber": 151,
"customername": "Muscle Machine Inc",
"contactlastname": "Young",
"contactfirstname": "Jeff",
"phone": null,
"addressline1": null,
"addressline2": null,
"city": null,
"state": null,
"postalcode": null,
"country": null,
"salesrepemployeenumber": null,
"creditlimit": null
},
{
"customernumber": 181,
"customername": "Vitachrome Inc.",
"contactlastname": "Frick",
"contactfirstname": "Michael",
"phone": null,
"addressline1": null,
"addressline2": null,
"city": null,
"state": null,
"postalcode": null,
"country": null,
"salesrepemployeenumber": null,
"creditlimit": null
},
{
"customernumber": 424,
"customername": "Classic Legends Inc.",
"contactlastname": "Hernandez",
"contactfirstname": "Maria",
"phone": null,
"addressline1": null,
"addressline2": null,
"city": null,
"state": null,
"postalcode": null,
"country": null,
"salesrepemployeenumber": null,
"creditlimit": null
},
{
"customernumber": 456,
"customername": "Microscale Inc.",
"contactlastname": "Choi",
"contactfirstname": "Yu",
"phone": null,
"addressline1": null,
"addressline2": null,
"city": null,
"state": null,
"postalcode": null,
"country": null,
"salesrepemployeenumber": null,
"creditlimit": null
}]

转换后,观察其中的区别

[{
"customernumber": "131",
"customername": "Land of Toys Inc.",
"contactlastname": "Lee",
"contactfirstname": "Kwai"
},
{
"customernumber": "151",
"customername": "Muscle Machine Inc",
"contactlastname": "Young",
"contactfirstname": "Jeff"
},
{
"customernumber": "181",
"customername": "Vitachrome Inc.",
"contactlastname": "Frick",
"contactfirstname": "Michael"
},
{
"customernumber": "424",
"customername": "Classic Legends Inc.",
"contactlastname": "Hernandez",
"contactfirstname": "Maria"
},
{
"customernumber": "456",
"customername": "Microscale Inc.",
"contactlastname": "Choi",
"contactfirstname": "Yu"
}]

四、关于两个Bean的相互转换,更简单些,这里贴上方法,自己有兴趣可以试试

五、Hutool包中的JsonUtil实现的对象的互转都是基于Json的