stream流的常见使用

过滤为空的数据

freeScopeList().stream().
filter(f -> CollectionUtils.isNotEmpty(f.getFreeScopeFeeList())). //免租范围不能空
filter(f -> CollectionUtils.isNotEmpty(f.getFreePeriodList())). //免租期不能空
collect(Collectors.toList()))

收集集合里面的某个字段并去重

// 集团公司id
List list1 = homeBiCompanyDataList.stream().map(map1 -> map1.getGroupCompanyId()).distinct()
.collect(Collectors.toList());

收集集合里面的取某些字段,并用,拼接

String spaceFloorName = spaceDtoList.stream().map(m -> m.getBasFloorDto().getFloorName() + “-” + m.getSpaceName()).collect(Collectors.joining(“,”));

差集

// 最新数据的id
List list1 = boList.stream().map(map1 -> map1.getId()).distinct().collect(Collectors.toList());
// 历史数据的id
List list2 = assetList.stream().map(map1 -> map1.getId()).distinct().collect(Collectors.toList());
// 差集 (list2 – list1) 删除
List deleteList = list2.stream().filter(item -> !list1.contains(item)).collect(Collectors.toList());

分组

// 先对名字进行分组,然后按照年龄进行分组
Map<String, Map> map = Stream.of(
new Person(“张三”, new Date(), 175),
new Person(“李四”, new Date(), 177),
new Person(“张三”, new Date(), 165),
new Person(“李四”, new Date(), 166),
new Person(“张三”, new Date(), 182))
.collect(Collectors.groupingBy(Person::getName, Collectors.groupingBy(Person::getAge)));
map.forEach((k, v) -> System.out.println(“k=” + k + “ ” + “v=” + v));

list转map

Map map =
leaRuleFeeList.stream().collect(Collectors.toMap(LeaContRuleFeeBo::getFeeId, (k) -> k))

forEach 处理list数据

decorationFeeList.stream().forEach(fee -> {
fee.setOpenApproveId(openApproveId);
fee.setIsDeleted(IsDelEnum.IS_UN_DEL.getKey());
});

本站无任何商业行为
个人在线分享 » stream流的常见使用
E-->