欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

Java8 - 小想法

最编程 2024-05-01 12:53:45
...

实际生产中遇到的问题

list -------转化为 Map<String,List<>>
BiPredicate----巧用,用于过滤不满足的条件

 public void checkAmount(String mainId,List<PromItemAReq> promItemListReq,String status){ 
         // 规则设置的 限制数量
        List<TProductGroupBuyingSelectRuleDO> dbUserGrouplist = selectRuleDBTunnel.findModelByMainId(mainId); 
        dbUserGrouplist=dbUserGrouplist.stream().collect(Collectors.toList());
        Map<String, Integer>  dbugLimitMap = dbUserGrouplist.stream()
                .collect(toMap(TProductGroupBuyingSelectRuleDO::getUserGroupID,TProductGroupBuyingSelectRuleDO::getLimitCount));
        // Map <用户选中的 用户组- 商品list>
        Map<String, List<PromItemAReq>>  ugProductMap = promItemListReq.stream().collect(Collectors.groupingBy(PromItemAReq::getUserGroupID));
        // 如果limitcount = =传入的count-------true
        BiPredicate<Integer,Integer> pricePred = (x,y) -> (ModelConvertorUtil.compareInt(x,y));
        for (String userGroupId : dbugLimitMap.keySet()) {
            // 如果传入的用户组的
            Check.check(isEmpty(ugProductMap.get(userGroupId)),"提交失败,活动商品不满足限制数量。");
            int limitCountDb = dbugLimitMap.get(userGroupId);
            int reqCount = ugProductMap.get(userGroupId).size();
            Check.check(!pricePred.test(limitCountDb,reqCount),"提交失败,活动商品不满足限制数量。");
        }
    }

怎么去重

List<Student> list = Students.stream()
.collect(collectingAndThen(toCollection(() -> new TreeSet<>(comparingLong(Student::getId))), ArrayList::new));

List<ClassEntity> distinctClass = classEntities.stream().
collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.getProfessionId() + ";" + o.getGrade()))),

 ArrayList::new));