【Lambda】lambda的list用法记录

> 根据实体类的某个字段去重

userList.stream().collect(Collectors.collectingAndThen(
    Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(User::getUsername))), ArrayList::new
));

> list 转 map

Map userMap = userList.stream().collect(
    Collectors.toMap(UserListDto::getId, UserListDto::getName)
);
Map> groupByOrgCode = data.stream().collect(
    Collectors.groupingBy(OrgImport::getOrgCode)
);
Map collect = contractList.stream().collect(
    Collectors.toMap(ContractVo::getSymbol, paperContractVo -> ContractVo)
);

> 根据实体类的某个字段求和

BigDecimal total = computeList.stream()
    .map(parcel -> parcel.getNumBbl())
    .reduce(BigDecimal.ZERO, BigDecimal::add);

你可能感兴趣的:(java)