如何實現兩個Bean或兩個List的快速,相互轉換

一、場景中,兩個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的

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章