stream流的常用操作

1.提取对象集合某一字段(list 转list)

 List<String> codeList = MatePriceWhiteVoList.stream().map(MatePriceWhiteVo::getStoreCode).distinct().collect(Collectors.toList());
 //其中 distinct()是去重

2.list 转换为 map

//1.取某个字段为key  某个值为value
Map<String, String> codeMap = listShopList.stream().filter(ObjectUtils::isNotEmpty).collect(Collectors.toMap(ShopReportVo::getCode, ShopReportVo::getName));
//其中 filter(ObjectUtils::isNotEmpty)是保留对象不为空的

//2.取某个字段为key  对象为value
 Map<String,ShopReportVo> map=listShopList.stream().collect(Collectors.toMap(ShopReportVo::getName,Function.identity()));
 //或者
 Map<String,ShopReportVo> map=listShopList.stream().collect(Collectors.toMap(ShopReportVo::getName,shopReportVo -> shopReportVo));

//3.取某个字段为key  多个对象为value (根据key分组)
Map<String,List<ShopReportVo>> map=listShopList.stream().collect(Collectors.groupingBy(ShopReportVo::getName));

3.对list的操作(暂时只写了排序和过滤)

List<ShopReportVo> codeList =listShopByCodes.getResult().stream()
                            .filter(shopReportVo -> "A".equals(shopReportVo.getCode()))
                            .sorted(Comparator.comparing(ShopReportVo::getPlatformShopId).reversed()
                                    .thenComparing(ShopReportVo::getName)
                                    .thenComparing(ShopReportVo::getTagName))
                            .collect(Collectors.toList());

//过滤filter()      保留 code为A的对象     
//排序sorted()      根据platFormShopId 倒序排序   reversed()是取反(之前的排序全部翻过来,单个排序或者同正序同倒序可以当作排序用)  默认是正序
//thenComparing() 多个排序条件 如果第一个排序条件无法区分 则用后面的排序  

//还有一种排序写法 (个人不太喜欢)
 // naturalOrder正排   reverseOrder倒排
 //nullsFirst	正排,如果有null,则null在最前(如果是上述排序方式 出现null,会报错,加了就不会)
 // nullsLast	倒排,如果有null,则null在最后(与nullsFirst类似)
   List<ShopReportVo> codeList =listShopByCodes.getResult().stream()
                            .filter(shopReportVo -> "A".equals(shopReportVo.getCode()))
                            .sorted(Comparator.comparing(ShopReportVo::getPlatformShopId,Comparator.reverseOrder())
                                    .thenComparing(ShopReportVo::getName, Comparator.naturalOrder()))
                            .collect(Collectors.toList());
                           
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值