java8引入的stream包不知道大家使用过没有,处理集合真的超级方便,目前工作中用到的stream包比较少,以下是我用到过的几个场景,希望可以帮助到大家。
例:获取当前集合下urid的集合
List beanList = new ArrayList<>();
List uridList = beanList.stream().map(ResBean::getUrid).collect(Collectors.toList());
例:通过年月分组,2023-03、2023-04、2023-05 这里我的Repaydate字段是String类型的
List beanList = new ArrayList<>();
Map> dateToMapList = beanList .stream().collect(Collectors.groupingBy(ResBean::getRepaydate));
例:过滤出当前集合下组织id=aaa的数据
String orgId = "aaa"
List beanList = new ArrayList<>();
List sybBeanList = beanList.stream().filter(b -> orgId.equals(b.getOrgid())).collect(Collectors.toList());
例:求出当前集合下Principal字段的合计
List beanList = new ArrayList<>();
BigDecimal principalSum = beanList.stream().map(ResBean::getPrincipal).reduce(BigDecimal.ZERO, BigDecimal::add);