开发中经常碰到多个list整合成一个的情况。
比较通俗易懂的做法是:
List<String> businessLicense = accessoryDto.getBusinessLicense();
List<String> agencyDocument = accessoryDto.getAgencyDocument();
List<String> otherDocuments = accessoryDto.getOtherDocuments();
List<String> productsReport = accessoryDto.getProductsReport();
List<String> threeYearsAchievements = accessoryDto.getThreeYearsAchievements();
List<String> fileIdList = new ArrayList<>();
fileIdList.addAll(businessLicense);
fileIdList.addAll(agencyDocument);
fileIdList.addAll(otherDocuments);
fileIdList.addAll(productsReport);
fileIdList.addAll(threeYearsAchievements);
使用stream实现就比较优雅了
List<String> fileIdList = ListIntegration(businessLicense,agencyDocument,otherDocuments,productsReport,threeYearsAchievements);
public List<String> ListIntegration(List<String> ...lists){
return Arrays.stream(lists).flatMap(List::stream).collect(Collectors.toList());
}
本文介绍了在Java开发中如何将多个列表整合成一个。传统的做法是逐个添加到新的列表中,而更优雅的方式是使用Java 8的Stream API,通过flatMap和collect方法实现列表的合并。这种方法简洁且高效。
2967

被折叠的 条评论
为什么被折叠?



