java集合常用方法汇总

21 篇文章 0 订阅
文章介绍了如何在Java中使用List进行新建、赋值、遍历、字符串处理、分组以及类型转换的操作,包括流API的运用,如map、filter、collect和split等。
摘要由CSDN通过智能技术生成

集合排序链接
集合排序链接

1.新建集合

List<String> result = Lists.newArrayList();

2.集合赋值

List<String> orderStatusList = Lists.newArrayList("60", "70", "90");

3.遍历集合

list.stream().map(CspTransferOrderInfo::getTransferOutOrderCode).filter(StringUtils::isNotBlank).collect(Collectors.toList());

4.集合逗号分割为字符串

StringUtils.join(transferOutOrderCodeList, "、")
String.join(",", cspShop.getWarehouseCodeList()
String equipmentNumberList = list.stream().map(CspWarehouseEquipment::getEquipmentNumber).collect(Collectors.joining(","));

5.Stirng 转 List

CommonUtils.stringChangeList(cspCustomerOrderInfo.getOutOrderCode())
List<String> cityCodeList = Arrays.asList(serviceProductManageBean.getDestinationCode().split(","));

6.集合分组

Map<String, List<CscCustomerInfo>> listMap = customerInfoList
                .stream()
                .collect(Collectors.groupingBy(CscCustomerInfo::getPostCode));

7.集合分组+转map集合

List<InterfaceMappingConfigurationDetails> mappingDetailsList = iInterfaceMappingConfigurationDetailsService.selectInterfaceMappingConfigurationDetailsList(selectInterfaceMappingDetails);
        Map<String, Map<String, String>> groupByMappingType = mappingDetailsList
                .stream()
                .collect(Collectors.groupingBy(
                                InterfaceMappingConfigurationDetails::getMappingType,
                                Collectors.toMap(InterfaceMappingConfigurationDetails::getOmsCode,
                                        InterfaceMappingConfigurationDetails::getErpCode,(v1,v2)->v1)
                        )
                );

8.List转List

/**
     * 集合类型转换
     *
     * @param longList 参数
     * @return 结果
     */
    private static List<String> convertToStringList(List<Long> longList) {
        List<String> stringList = new ArrayList<>();

        for (Long num : longList) {
            stringList.add(num.toString());
        }

        return stringList;
    }

9.List转List

String destinationCode = '12,1233,4444,32222,';
List<Long> indexIds = Arrays.stream(destinationCode.split(",")).map(Long::parseLong).collect(Collectors.toList());

10.对象转集合

Collections.singletonList(crmAllFiles.get(0))

11.返回空集合

if (CollectionUtils.isEmpty(dispatchDetailMonitorList)) {
                return Collections.emptyList();
            }

12.根据多个字段组合进行分组

Map<String, BmsYsbillcodeinfo> codeInfoMapsByIO = ysbillcodeinfos.stream().collect(Collectors.toMap(e -> e.getRelateCode() + e.getCodeType(), Function.identity()));

13.根据某个字段对集合进行去重

List<CspCustomerOrderDetails> rderDetails = new ArrayList<>(rderDetails.stream()
                                .collect(Collectors.toMap(CspCustomerOrderDetails::getGoodsCode,
                                        order -> order, // 元素作为 value
                                        (existing, replacement) -> existing))
                                .values());

// 根据外部订单号和商品编码进行去重
        rderDetails = new ArrayList<>(rderDetails.stream()
                .collect(Collectors.toMap(order -> order.getGoodsCode() + "-" + order.getOutOrderCode(),
                        Function.identity(), //
                        (existing, replacement) -> existing))
                .values());

14.hash

HashMap<Long, BaseDataTChinaCity> chinaCityMap = baseDataTChinaCities
                .stream()
                .collect(HashMap::new, (a, b) -> a.put(b.getIndexId(), b), HashMap::putAll);

15.获取集合中报价方式1和报价方式2相同的对象,并给出重复提示

Map<String, List<BaseDataQuotationStructureSet>> duplicateMap = baseDataQuotationStructureSetList.stream()
    .collect(Collectors.groupingBy(bdqs -> bdqs.getQuotationMethodOne() + "-" + bdqs.getQuotationMethodTwo()));
List<BaseDataQuotationStructureSet> duplicates = duplicateMap.values().stream()
    .filter(list -> list.size() > 1)
    .flatMap(Collection::stream)
    .collect(Collectors.toList());

16.遍历分组的数据

for (Map.Entry<String, List<GetHistoryTemperatureInfoDto>> entry : temperatureInfoDtoMap.entrySet()) {
                List<GetHistoryTemperatureInfoDto> list = temperatureInfoDtoMap.get(entry.getKey());
                Map<String, Object> map = new HashMap<>(list.size());
                list.forEach(dto -> map.put(dto.getId(), dto));
                esOperationClient.bulkDataAsync("temperature_" + entry.getKey(), map);
            }

17.复制实体类

BeanUtils.copyProperties(appletUserExtension, shopAppletUserExtensionVo);
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

东方巴黎~Sunsiny

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值