Loading... ## 1. 原因与问题 #### 1.1 原因 日常开发中有时候需要将某个对象集合的某个属性提取出来作为一个新的集合使用, 所以记录下来 #### 1.2 描述 现在有一个叫 `companyEmployeeList` 集合 我们需要将里面的 `employeeId` 提取出来作为一个新的集合 ``` List<CompanyEmployee> companyEmployeeList = companyEmployeeMapper.selectCompanyEmployeeNotUsedById(idArray); ``` ## 2. 解决方法 #### 2.1 .collect(Collectors.toList()) 转为List集合 ``` List<String> list = companyEmployeeList.stream().map(t -> t.getEmployeeId().toString()).collect(Collectors.toList()); ``` #### 2.2 .toArray 转为数组集合 ``` String[] list = companyEmployeeList.stream().map(t -> t.getEmployeeId().toString()).toArray(String[]::new); ``` #### 2.3 一个题外小方法 ``` /* 去重 */ companyEmployeeList.stream().distinct().collect(Collectors.toList()); ``` Last modification:February 28, 2022 © Allow specification reprint Support Appreciate the author AliPayWeChat Like 0 感谢大佬投喂 啾咪~