Java合并List集合对象的属性

Java合并List集合对象的属性

1. 业务概述

在常见的业务需求开发中会有这样一种场景,需要的数据集合来源有3种不同的来源,并且返回的都是List集合,现在需要做的是将这3个List集合中的对象属性合并,不是集合对象合并。
例如Store对象有四个字段,id,name,location(位置),peice(价格),但是name,location,price这3个字段需要三个接口获取,分别返回三个集合List< Store >,现在就是把这三个List< Store >转化为1个List< Store >,而不是把三个集合合并。

2. 原理分析

List storeListName; //姓名集合
List storeListItude;//经纬度集合
List storeListMinPrice;//价格集合

例如每个集合3条数据,都包含主键id
1.通过addAll方式合并:会有9条数据集合。
2.遍历赋值方式实现: 合并后3条完整数据集合。

3. 实现验证

Store

package com.zrj.unit.collection;

import lombok.Builder;
import lombok.Data;

import java.io.Serializable;

/**
 * @author zrj
 * @since 2021/8/13
 **/
@Data
@Builder
public class Store implements Serializable {
    private static final long serialVersionUID = 3629770788858566301L;

    /**
     * 主键id
     */
    private Long storeId;
    /**
     * 名称
     */
    private String storeName;

    /**
     * 经纬度
     */
    private Float tencentLongitude;

    private Float tencentLatitude;


    /**
     * 酒店最低房型价格
     * BigDecimal,为了方便测试采用Float
     */
    private Float minPrice;

}

CollectionUtils

package com.zrj.unit.collection;

import cn.hutool.core.bean.BeanUtil;
import com.google.common.collect.Lists;
import org.junit.Test;

import java.util.List;

/**
 * 合并集合的对象属性
 *
 * @author zrj
 * @since 2021/8/13
 **/
public class CollectionUtils {
    private static List<Store> storeListName = Lists.newArrayList();
    private static List<Store> storeListItude = Lists.newArrayList();
    private static List<Store> storeListMinPrice = Lists.newArrayList();

    public static void main(String[] args) {
        mergeList();
    }

    /**
     * 合并集合的对象属性
     */
    public static List<Store> mergeList() {
        buildList();
        List<Store> storeMergeList = Lists.newArrayList();
        storeMergeList.addAll(storeListName);

        storeMergeList.forEach(storeMerge -> storeListItude.forEach(storeItude -> {
            if (storeMerge.getStoreId().equals(storeItude.getStoreId())) {
                storeMerge.setTencentLatitude(storeItude.getTencentLatitude());
                storeMerge.setTencentLongitude(storeItude.getTencentLongitude());
            }
        }));

        storeMergeList.forEach(storeMerge -> storeListMinPrice.forEach(storePrice -> {
            if (storeMerge.getStoreId().equals(storePrice.getStoreId())) {
                storeMerge.setMinPrice(storePrice.getMinPrice());
            }
        }));

        return storeMergeList;
    }

    /**
     * 测试list的addAll
     * 合并的是集合对象,不是对象的属性
     * 每个List有3个对象,addAll之后就是9个对象
     */
    public static List<Store> AddAllList() {
        buildList();
        List<Store> storeMergeList = Lists.newArrayList();
        storeMergeList.addAll(storeListName);
        storeMergeList.addAll(storeListItude);
        storeMergeList.addAll(storeListMinPrice);

        return storeMergeList;
    }

    /**
     * 构建集合
     */
    public static void buildList() {
        storeListName.add(Store.builder().storeId(1L).storeName("金陵智慧酒店").build());
        storeListName.add(Store.builder().storeId(2L).storeName("玄武智慧酒店").build());
        storeListName.add(Store.builder().storeId(3L).storeName("秦淮智慧酒店").build());

        storeListItude.add(Store.builder().storeId(1L).tencentLongitude(181.12F).tencentLatitude(30.24F).build());
        storeListItude.add(Store.builder().storeId(2L).tencentLongitude(182.66F).tencentLatitude(31.25F).build());
        storeListItude.add(Store.builder().storeId(3L).tencentLongitude(183.88F).tencentLatitude(32.26F).build());

        storeListMinPrice.add(Store.builder().storeId(1L).minPrice(996.32F).build());
        storeListMinPrice.add(Store.builder().storeId(2L).minPrice(997.32F).build());
        storeListMinPrice.add(Store.builder().storeId(3L).minPrice(998.32F).build());
    }

    /**
     * BeanUtil对象拷贝是覆盖
     * store1覆盖store2
     *
     * @param
     * @return void
     */
    @Test
    public void copyTest() {
        Store store1 = Store.builder().storeId(1L).storeName("金陵智慧酒店").build();
        Store store2 = Store.builder().storeId(1L).minPrice(12.56F).build();

        // store1覆盖store2
        BeanUtil.copyProperties(store1, store2);

        System.out.println(store1);
        System.out.println(store2);
    }
}

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值