List集合流Api操作

1.集合分组单个对象value转为Map

 Map attrManageDTOMap = vehicleAttrManageDTOList
                .stream()
                .filter(s -> StringUtils.isNotBlank(s.getAttrValueId())).collect(toMap(VehicleAttrManageDTO::getAttrId, Function.identity(), (k1, k2) -> k2));

2.集合转为Map

 Map> maps 
 =list.stream().collect(Collectors.groupingBy(VehicleAttrManageDTO::getVinCode));

3.集合根据某个字段生成新的List集合

      List vinCodes =       
operatingNewCarSimpleList.stream().
map(OperatingNewCarSimple::getVinCode).collect(toList();

4. 集合根据某个字段生成新的Set集合(值去重)

      Set attrIds =
      queries.stream().
      map(OperatingAttributeQuery::getAttrId).collect(toSet());

5.集合进行过滤

 List multiAttrList =
           item.getValue().stream()
           .filter(value -> !StringUtils.isEmpty(value.getAttrValueId()) && 
            Arrays.stream(value.getAttrValueId().split(",")).count() > 1)
                            .collect(toList());

6.集合判断非空抛异常

 Assert.isTrue(!CollectionUtils.isEmpty(updateVO.getCarIds()),"carId不能为空");

7.集合值多个生成字符多个以逗号分隔

String stationCodes = dataList.stream().map(Station::getStationCode)
.collect(Collectors.joining(","));

 8.集合进行数量累加求和

Long shouldCountSum = asnItems.stream()
                      .map(OutBoundShippingNoticeItemPo::getCurrQuantity)
                      .reduce(Long::sum).get();

9. 集合按照id集合过滤对象集合返回新的集合

returnList = proxyDtos.stream().filter(proxyDto->{
      for(Long id:idList){
        if(proxyDto.getId().equals(id)){
          return true;
        }
      }
      return false;
    }).collect(Collectors.toList());

10.集合中按照某个字段值正序排列集合对象

 List sorted = 
 realCostDtos.stream().sorted(Comparator.comparingInt(BillingRealCostDto::getMonth))
 .collect(toList());

11.集合快速过滤设值

list.stream().forEach(item-> {
item.setEnabledName(item.getEnabled() ? "启用":"禁用");
});

12.集合按照集合里的参数 拼接成目标集合

List improtSnList = list.stream().map(d ->d.getMaterialCode()+"_"+d.getMaterialSn()).collect(Collectors.toList());

13.集合分批批量新增(一批执行200条)

 /**
   * 批量新增
   * 200条一批插入
   */
@Transactional
public Integer batchInsert(List vehicleShiftTaskVehicles) {
        List> partition = 
       Lists.partition(vehicleShiftTaskVehicles, 200);
        LongAdder result = new LongAdder();
        partition.forEach(it -> {
            int saveRes = vehicleShiftTaskVehicleMapper.batchInsert(it);
            result.add(saveRes);
        });
        return result.intValue();
	}

你可能感兴趣的:(Java,前端,html,java)