数据库 foreach union all 查询 拉姆达表达式,拉姆达表达式

代码

	String accountId = LoginUtils.getLoginAccountId();
		log.info("BaseUserCollectInfoServiceImpl.getMerchantInfoList ; 查询收藏商家列表开始  accountId: {}", accountId);
		PageInfo<BaseMerchantInfoResp> pageInfo = PageHelper.startPage(req.getPageNum(), req.getPageSize())
				.doSelectPageInfo(() -> userCollectInfoMapper.getMerchantInfoList(accountId));
		if (pageInfo.getTotal() == 0) {
			return PageInfoUtils.emptyPageInfo(req);
		}
		List<BaseMerchantInfoResp> respList = pageInfo.getList();
		List<String> accountIds = respList.stream().map(BaseMerchantInfoResp::getAccountId).collect(Collectors.toList());
		if (CollectionUtils.isEmpty(accountIds)) {
			return pageInfo;
		}
		//todo 查询商家服务列表, 未上架的不能查询
		List<BaseMerchantServeResp> merchantServeList = userCollectInfoMapper.getBaseMerchantServeList(accountIds);
		Map<String, List<BaseMerchantServeResp>> map = merchantServeList.stream().collect(Collectors.groupingBy(BaseMerchantServeResp::getMerchantAccountId, Collectors.toList()));
		respList.forEach(e -> {
			e.setServeProjectNameList(JSON.parseArray(e.getServeProjectName(), String.class));
			e.setMerchantServeList(map.get(e.getAccountId()));
			e.setDistance(NumberUtils.getDistance(req.getLongitude(), req.getLatitude(), e.getLongitude(), e.getLatitude()));
		});
		pageInfo.setList(respList);
		return pageInfo;
	}

    <select id="getBaseMerchantServeList" resultType="com.first.pet.base.user.vo.BaseMerchantServeResp">
        <foreach item="id" collection="ids" separator="union all">
            (select id,serve_name, serve_type_title ,discount_price,merchant_account_id,origin_price from base_merchant_serve where
            merchant_account_id =
            #{id} and yn = 1 and is_show = 'NORMAL' order by id desc limit 2)
        </foreach>
    </select>

拉姆达表达式判空操作

	
	有时候,希望得到的map的值不是对象,而是对象的某个属性,那么可以用下面的方式:
Map<Long, String> maps = userList.stream().collect(Collectors.toMap(User::getId, User::getAge, (key1, key2) -> key2));
//判空 转map  value为属性	
Map<Integer, Integer> brandCountData = brandCount.stream().collect
				(Collectors.toMap(e -> e.getBrandId() != null ? e.getBrandId() : 0, BrandCountDTO::getCount, (key1, key2) -> key2));

//判断值重复
List<OrderServiceDTO> orderServiceDTOList = orderServiceExport.getOrderServiceDTO(orderIds);
		Map<String, OrderServiceDTO> orderServiceDTO = orderServiceDTOList.stream().collect(Collectors.toMap(OrderServiceDTO::getOrderId, o -> o, (old, ne) -> old));
		
//判空 转map  value为对象		
Map<String, List<BrandCountDTO>> brandCountData = brandCount.stream().collect
(Collectors.groupingBy(e -> StringUtils.isNotBlank(e.getBrandId()) ? e.getBrandId() : "", Collectors.toList()));

//获取集合中的对象的某个值返回
  List<ResourceInfoDto> result=new ArrayList<>();
  platformResourceInfoResps.forEach(i->result.add(new ResourceInfoDto().setDesc(i.getTitle()).setId(i.getId())));

//集合返回组装对象
//第一种方式
List<GoodsSpecification> list = this.lambdaQuery().eq(GoodsSpecification::getGoodsId, id)
			.eq(GoodsSpecification::getYn, YnEnums.VALID.getCode()).list();
	if (CollectionUtils.isEmpty(list)) {
		return Collections.emptyList();
	}
	List<GoodsSpecificationDetailDataResp> collect = list.stream().map(e ->
	{
		GoodsSpecificationDetailDataResp resp = BeanUtil.copyBean(e, GoodsSpecificationDetailDataResp.class);
		return resp;
	}).collect(Collectors.toList());
//第二种方式
 List<BaseUserInfo> infos = baseUserInfoService.lambdaQuery().
 eq(BaseUserInfo::getYn, YnEnums.VALID.getCode()).in(BaseUserInfo::getAccountId, accountIds).list();
        return infos.stream().map(b -> BeanUtil.copyBean(b, BaseUserNameDTO.class)).
        collect(Collectors.toList());

//list转map
public static void main(String[] args) {
        List<User> userList = Lists.newArrayList();//存放user对象集合

        User user1 = new User(1L, "张三", 24);
        User user2 = new User(2L, "李四", 27);
        User user3 = new User(3L, "王五", 21);

        userList.add(user1);
        userList.add(user2);
        userList.add(user3);

        //ID为key,转为Map
        Map<Long,User> userMap = userList.stream().collect(Collectors.toMap(User::getId, a -> a,(k1, k2)->k1));
        System.out.println(userMap);
    }

遍历map
JDK 8 提供了 Lambda 表达式支持, 其语法看起来更简洁, 可以同时拿到 key 和 value.

不过, 经过简单的测试, Lambda 表达式遍历 Map 的速度要低于 entrySet 遍历的方式, 所以更推荐用 entrySet 去遍历 Map.

    /** Lambda 获取key and value */
    public void testLambda() {
        map.forEach((key, value) -> {
            System.out.println(key + ":" + value);
        });
    }

    参考链接
https://blog.csdn.net/qq_42380734/article/details/105373696

list根据对象的属性值去掉重复元素
在这里插入图片描述

		List<Personn> list2 = Arrays.asList
				(new Personn("Alice", 25,"123"), new Personn("Charlie", 35,"234"), new Personn("Dave", 40,"345"),
						new Personn("Alice", 25,"567"));



		ArrayList<Personn> collect1 = list2.stream().collect(Collectors.collectingAndThen(
				Collectors.toCollection(() -> new TreeSet<>(
						Comparator.comparing(p->p.getName() + ";" + p.getAge()))), ArrayList::new));

参考链接

list转map,相同的某个值作为key,list作为值相同的所对应的对象的集合

        // 根据订单id查询商品信息
        List<String> orderIds = resps.stream().map(OrderGoodsListResp::getOrderId).collect(Collectors.toList());
        List<OrderGoodsItem> orderGoodsItems = orderGoodsItemService.lambdaQuery()
                .in(OrderGoodsItem::getOrderId,orderIds)
                .eq(OrderGoodsItem::getYn, YnEnums.VALID.getCode())
                .list();
        Map<String, List<OrderGoodsInfoResp>> listMap = orderGoodsItems.stream().map(o -> {
            OrderGoodsInfoResp orderGoodsInfoResp = new OrderGoodsInfoResp();
            orderGoodsInfoResp.setGoodsName(o.getGoodsName());
            orderGoodsInfoResp.setPurchaseVolume(o.getPurchaseVolume());
            orderGoodsInfoResp.setOrderId(o.getOrderId());
            orderGoodsInfoResp.setSpecificationName(o.getSpecificationName());
            return orderGoodsInfoResp;
        }).collect(Collectors.groupingBy(OrderGoodsInfoResp::getOrderId));
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值