Java常用方法(一)列表

原文地址

原文链接

初始化

//返回List对象不可改变, 都没有定义List的连接方式也可以知道这种初始化是不能改变内部数据的
Arrays.asList(1, 2, 3);
//new Arrayy addAll 的方式
new ArrayList<>(Arrays.asList(1, 2, 3));
new LinkedList<>(Arrays.asList(1, 2, 3));
//stream 由数组生成自然是连续结构
Arrays.stream(new int[]{1,3,4}).boxed().collect(Collectors.toList());
//转数组 points -> ArrayList<Point> points
Point[] pointArray = new MultiPoint(points.toArray(new Point[0]);

求和

dto.setSaleNum(orderMessageDtoList.stream().mapToInt(OrderMessageDto::getNum).sum());
BigDecimal totalPrice = list.stream().map(Apple::getPrice).reduce(BigDecimal.ZERO, BigDecimal::add);

分组

        List<FamilyBookDeliveryCardEntity> allCardList = source.getAllCardList();
        Map<Long, List<FamilyBookDeliveryCardEntity>> parentToCardMap =
            allCardList.stream().collect(Collectors.groupingBy(FamilyBookDeliveryCardEntity::getParentCardId));

            Map<String, List<HouseDto>> villageHouseMap
                    = houseDtoList.stream().collect(Collectors.groupingBy(cube ->
                    cube.getDivisionFullName() + "_" + cube.getStreet() + "_" + cube.getVillage()));

除去特定对象

cubeItemBoList.removeIf(cubeItemBO -> cubeItemBO.getStatus().equals(ItemStatusEnum.DISABLED.getValue()));

查询,正向条件

List<FamilyBookDeliveryCardEntity> childCardEntity = entityList.stream()
    .filter(entity -> parentEntity.getId().equals(entity.getParentCardId()))
    .collect(Collectors.toList());

查重

if (optList.size() != optList.stream().distinct().count()) {  
    throw ErrorCode.BAD_PARAM.message("选项元素不能重复").exception();  
}  

去重

//简单去重
List<Integer> listWithoutDuplicates = numbersList.stream().distinct().collect(Collectors.toList());
//根据propertyId去重  
List<SkuPropertyEntity> skuPropertyEntityListAlone = skuPropertyEntityList.stream().  
        collect(Collectors.collectingAndThen(Collectors.toCollection(() ->  
                new TreeSet<>(Comparator.comparing(SkuPropertyEntity::getPropertyId))), ArrayList::new));
//多字段去重
List<PromotionSkuModel> promotionSkuModelDistinctList = promotionSkuModelList
    .stream()
    .collect(Collectors
             .collectingAndThen(Collectors
                                .toCollection(()
                                              -> new TreeSet<>(Comparator
                                                               .comparing(o -> o.getPromotionId() + ";" + o.getSkuId()))), ArrayList::new));

排序

//List内对象根据属性排序
List<CubeSessionItemBO> cubeSessionItemBoSortList = cubeSessionItemBOList.stream().sorted(Comparator.comparing(CubeSessionItemBO::getItemId)).collect(Collectors.toList());
//List中所有对象中某个属性值最大的对象
            Optional<FamilyBookDeliveryCardEntity> maxInvalidOptional =
                    entityList.stream().max(Comparator.comparing(FamilyBookDeliveryCardEntity::getInvalidTime));
//多层排序
return cardBaseMessageVoList.stream().sorted(Comparator
                .comparing(CardBaseMessageVo::getExpireTime)
                .thenComparing(CardBaseMessageVo::getStatus, Comparator.reverseOrder())
                .thenComparing(CardBaseMessageVo::getId))
                .collect(Collectors.toList());

插入

//List<Long>中插入字符分割
String result = Joiner.on(",").join(skuList);

对象转换

//多属性对象转少属性对象
List<CartDTO> cartDTOList = new ArrayList<>();
for (OrderDetail item : orderDetailList){
  cartDTOList.add(new CartDTO(item.getProductId(),item.getProductQuantity()));
}
//->
List<CartDTO> cartDTOList = orderDetailList.stream()
                .map(e -> new CartDTO(e.getProductId(), e.getProductQuantity()))
                .collect(Collectors.toList());
//单个处理
List<Long> productIdList = orderDetailList.stream().map(CartDTO::getProductId).collect(Collectors.toList()));
//List转map
            Map<String, SysDictType> map = typeList.stream().collect(
                    Collectors.toMap(SysDictType::getId, Function.identity() ,(key1 , key2)-> key2 ));
          Map<String, List<HrHousePrAudit>> map = hrHousePrAuditList.stream().collect(
                    Collectors.groupingBy(HrHousePrAudit::getHousePrId));
        Map<String, List<UnpushedRoomResults>> dataMapGroupByBuild = dataList.stream()
                .collect(Collectors.groupingBy(data-> data.getBuildingNum()+data.getUnit()));

其他流处理

stream

原文地址

原文链接

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值