1,跟据某个属性分组OfficeId
Map<String, List<IncomeSumPojo>> collect = list.stream().collect(Collectors.groupingBy(IncomeSumPojo::getOfficeId));
2,根据某个属性分组OfficeId,汇总某个属性Money
Map<String, Double> collect = list.stream().
collect(Collectors.groupingBy(IncomeSumPojo::getOfficeId,Collectors.summingDouble(IncomeSumPojo::getMoney)));
3,根据某个属性添加条件过滤数据,
list = list.stream().filter(u -> !u.getAmount().equals("0.00")).collect(Collectors.toList());
4,判断一组对象里面有没有属性值是某个值
List<Menu> menuList = UserUtils.getMenuList();
boolean add = menuList.stream().anyMatch(m -> "plan:ctPlan:add".equals(m.getPermission()));
5,取出一组对象的某个属性组成一个新集合
List<String> tableNames=list.stream()
.map(User::getMessage)
.collect(Collectors.toList());
6.List 去重
List<String> list = useridList.stream()
.distinct()
.collect(Collectors.toList());
List<String> itemCodes = itemList.stream().map(OrderRebateMateGroupItemResponseDTO::getItemCode)
.distinct().collect(Collectors.toList());
7.java stream中的findFirst
返回出现的第一个结果
List<String> lst1 = Arrays.asList("Jhonny", "David", "Jack", "Duke", "Jill","Dany","Julia","Jenish","Divya");
Optional<String> findFirst = lst1.parallelStream().filter(s -> s.startsWith("D")).findFirst();
System.out.println(findFirst.get()); //总是打印出David
8.stream自定义key,返回map
Map<String, AttStatisticsDailyResponseDTO> dailyRespMap = Optional.ofNullable(dailyRespList)
.orElse(Collections.emptyList())
.stream()
.collect(Collectors.toMap(dailyResp -> {
String keyPrefix = dailyResp.getOrgId() + "@" + dailyResp.getLoginId() + "@" + dailyResp.getStatisticsDate();
String workType = dailyResp.getWorkType() == null ? "NO" : dailyResp.getWorkType();
String key = keyPrefix + "@" + workType;
log.info("打卡数据放到缓存的key={}", key);
return key;
//相同key,覆盖,取最后的值
}, a->a,(V1,V2)->V1));
9.通过某个字段返回map
userList.stream().collect(Collectors.toMap(User::getId, User::getName));
10.多个字段进行分组并统计数量
List<PefsAdvanceOrderHeadReqDTO> list = new ArrayList<>();
PefsAdvanceOrderHeadReqDTO req = new PefsAdvanceOrderHeadReqDTO();
req.setSalesman("A");
req.setSalesmanCode("A1");
req.setTotal(1);
list.add(req);
req = new PefsAdvanceOrderHeadReqDTO();
req.setSalesman("A");
req.setSalesmanCode("A1");
req.setTotal(2);
list.add(req);
req = new PefsAdvanceOrderHeadReqDTO();
req.setSalesman("B");
req.setSalesmanCode("B1");
req.setTotal(1);
list.add(req);
Map<String, Double> collect = list.stream().collect(Collectors.groupingBy(o -> o.getSalesmanCode() + "@" + o.getSalesman(), Collectors.summingDouble(PefsAdvanceOrderHeadReqDTO::getTotal)));
System.out.println(JSON.toJSONString(collect));
11.JDK8的迭代方式
//JDK8的迭代方式
map.forEach((key, value) -> {
System.out.println(key + ":" + value);
});
- 通过某个字段 生成List
List<String> itemCodeList = list.stream().map(OrderCustDiscountItemResponseDTO::getItemCode).collect(Collectors.toList());
13 金额统计文章来源:https://www.toymoban.com/news/detail-514683.html
BigDecimal adjustAmount = Optional.ofNullable(reqList).orElse(Collections.emptyList()).stream()
.map(OrderFeeAdjustImportReqDTO::getAdjustAmount)
.reduce(BigDecimal.ZERO, BigDecimal::add);
14 根据某个属性去重文章来源地址https://www.toymoban.com/news/detail-514683.html
public List<RptDayMonthTargetVO> queryTarget() {
List<RptDayMonthTarget> list = targetService.selectMonthTarget();
//数据去重-根据Bean对象的SupplierId属性去重
List<RptDayMonthTarget> dbList = list.stream().filter(distinctByKey(RptDayMonthTarget::getSupplierId)).collect(Collectors.toList());return voList;
}
private static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
Map<Object,Boolean> seen = new ConcurrentHashMap<>();
return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
}
到了这里,关于根据list中的对象某个属性 进行分组的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!