合并两个bean的字段

合并两个bean的字段

  • 前言
  • 一、准备工作
    • 1、准备一个全是Object的对象...OdooResp
    • 2、准备一个固定属性的对象...User
  • 二、接口API
  • 三、测试结果
  • 总结


前言

说实话,这已经是工作上第二次遇到这个问题了…
虽然说这种需求很奇怪,但还是记录下。

需求概述:
1:对象a,合并两个bean的字段(id,name,fav)
2:对象b,没有明确的属性(属性个数不确定,属性类型不确定)
3:接口返回结果里,所有的字段都有


提示:以下是本篇文章正文内容,下面案例可供参考

一、准备工作

hutool,google.guava

1、准备一个全是Object的对象…OdooResp


@Data
public class OdooResp {
    private Object employee_id;
    private Object salary;
    private Object booleanFlag;
    private Object null_test;
    private Object job_id;
    private Object history;

    public OdooResp() {
    }

    public OdooResp(Object employee_id, Object salary, Object booleanFlag, Object null_test, Object job_id, Object history) {
        this.employee_id = employee_id;
        this.salary = salary;
        this.booleanFlag = booleanFlag;
        this.null_test = null_test;
        this.job_id = job_id;
        this.history = history;
    }
}

2、准备一个固定属性的对象…User


@Data
public class User {

    private Long id;

    private String name;

    private List<String> fav;

    public User() {
    }

    public User(Long id, String name, List<String> fav) {
        this.id = id;
        this.name = name;
        this.fav = fav;
    }
}

二、接口API

代码如下(示例):


@RestController
@RequestMapping("bean2map")
public class Bean2MapApi {

    @GetMapping(value = "demo", name = "将两个bean组合成一个map返回给前端")
    public Object demo() {
        List<Object> response = new ArrayList<>();

        List<OdooResp> odooRespList = getOdooRespList();
        List<Map<String, Object>> odooMapList = odooRespList.stream().map(BeanUtil::beanToMap).collect(Collectors.toList());

        List<User> userList = getUserList();
        
    	//前面都是造数据,getObject是重点
    	return userList.stream().map(user -> getObject(odooMapList, user)).collect(Collectors.toList());
    }

    private Object getObject(List<Map<String, Object>> odooMapList, User user) {
        Map<String, Object> odooMap = odooMapList.stream().filter(tempMap -> user.getId().equals(tempMap.get("employee_id"))).findFirst().orElse(null);
        if (CollectionUtils.isEmpty(odooMap)) {
            return user;
        }
        Map<String, Object> result = new HashMap<>();
        result.putAll(BeanUtil.beanToMap(user));
        result.putAll(odooMap);
        return result;
    }

    public static List<OdooResp> getOdooRespList() {
        List<OdooResp> odooRespList = new ArrayList<>();
        List<List<Object>> hository1 = Lists.newArrayList(Lists.newArrayList(2018, "kk", "不行"), Lists.newArrayList(2020, "cl", "一般"), Lists.newArrayList(2022, "yl", "从可以到不行"));
        List<List<Object>> hository2 = Lists.newArrayList(Lists.newArrayList(2016, "jd", "不行"), Lists.newArrayList(2017, "gc", "一般"), Lists.newArrayList(2022, "home", "从可以到不行"));

        odooRespList.add(new OdooResp(111L, new BigDecimal("108.50"), false, null, Lists.newArrayList(2222, "实习生"), hository1));
        odooRespList.add(new OdooResp(222L, new BigDecimal("4500.50"), true, Lists.newArrayList("最开始", "早恋了"), Lists.newArrayList("最后", "结婚了"), hository2));

        return odooRespList;
    }


    public static List<User> getUserList() {
        List<User> userList = new ArrayList<>();
        userList.add(new User(111L, "张三", Lists.newArrayList("吃饭", "睡觉", "打豆豆")));
        userList.add(new User(222L, "李四", Lists.newArrayList("动漫", "小说")));
        return userList;
    }
}

三、测试结果

[
    {
        "null_test": null,
        "job_id": [
            2222,
            "实习生"
        ],
        "employee_id": "111",
        "name": "张三",
        "fav": [
            "吃饭",
            "睡觉",
            "打豆豆"
        ],
        "id": "111",
        "history": [
            [
                2018,
                "kk",
                "不行"
            ],
            [
                2020,
                "cl",
                "一般"
            ],
            [
                2022,
                "yl",
                "从可以到不行"
            ]
        ],
        "salary": 108.50,
        "booleanFlag": false
    },
    {
        "null_test": [
            "最开始",
            "早恋了"
        ],
        "job_id": [
            "最后",
            "结婚了"
        ],
        "employee_id": "222",
        "name": "李四",
        "fav": [
            "动漫",
            "小说"
        ],
        "id": "222",
        "history": [
            [
                2016,
                "jd",
                "不行"
            ],
            [
                2017,
                "gc",
                "一般"
            ],
            [
                2022,
                "home",
                "从可以到不行"
            ]
        ],
        "salary": 4500.50,
        "booleanFlag": true
    }
]

总结

没有总结…

你可能感兴趣的:(java)