现有两个list List<TzglInvestmentPlanProjectEntity> planProjectList; List<TzglInvestmentPlanProjectEntity> planProjectList2; 对比两个list,取出planProjectList2中projectId在planProjectList中没有的
时间: 2024-01-13 08:03:26 浏览: 28
可以使用Java 8的流(Stream)和Lambda表达式来实现这个功能,具体代码如下:
```
List<TzglInvestmentPlanProjectEntity> result = planProjectList2.stream()
.filter(p2 -> planProjectList.stream().noneMatch(p1 -> p1.getProjectId().equals(p2.getProjectId())))
.collect(Collectors.toList());
```
这段代码首先将planProjectList2转化为流,然后使用filter方法过滤出projectId在planProjectList中不存在的元素,最后使用collect方法将结果转化为List并返回。其中,noneMatch方法用于判断planProjectList中是否存在与p2的projectId相同的元素。
相关问题
现有两个list List<TzglInvestmentPlanProjectEntity> planProjectList; List<TzglInvestmentPlanProjectEntity> planProjectList2; 对比两个list,取出planProjectList中projectId在planProjectList2中没有的
可以使用Java 8的流式操作来实现这个需求,具体代码如下:
```
List<TzglInvestmentPlanProjectEntity> differenceList = planProjectList.stream()
.filter(project -> planProjectList2.stream()
.noneMatch(project2 -> project.getProjectId().equals(project2.getProjectId())))
.collect(Collectors.toList());
```
上述代码中,首先使用stream()方法将planProjectList转换为一个流,然后使用filter()方法过滤出projectId在planProjectList2中没有的项目,最后使用collect()方法将过滤后的项目收集到一个新的List中。
需要注意的是,上述代码中的TzglInvestmentPlanProjectEntity需要根据实际情况进行替换。
public class TzglInvestmentPlanProjectEntity { private String projectId; private BigDecimal investmentTotalMoney; private BigDecimal investmentYearMoney; } 现有两个list List<TzglInvestmentPlanProjectEntity> planProjectList; List<TzglInvestmentPlanProjectEntity> planProjectList2; 两个list中projectId相同的数据,把planProjectList中的investmentTotalMoney和investmentYearMoney赋值给planProjectList2
可以使用循环遍历两个List,找到projectId相同的数据,然后将investmentTotalMoney和investmentYearMoney赋值给planProjectList2中对应的数据。可以按照以下步骤来实现:
```
for(TzglInvestmentPlanProjectEntity entity1 : planProjectList) {
for(TzglInvestmentPlanProjectEntity entity2 : planProjectList2) {
if(entity1.getProjectId().equals(entity2.getProjectId())) {
entity2.setInvestmentTotalMoney(entity1.getInvestmentTotalMoney());
entity2.setInvestmentYearMoney(entity1.getInvestmentYearMoney());
break;
}
}
}
```
这里使用了两个for循环来遍历两个List,如果找到了projectId相同的数据,则将对应的investmentTotalMoney和investmentYearMoney赋值给planProjectList2中对应的数据。注意,如果找到了相同的数据,可以使用break语句来提高效率。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![.zip](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)