實際生產中遇到的問題
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));