过滤相同数据 currentYearDeptNames=currentYearDeptNamesstream distinct collectCollectorstoList; 获取所有部门数据 list2 过滤掉list2不含list1的数据 ListJSONObject de
你可以使用stream的distinct方法来过滤掉重复的数据,并使用collect方法将结果收集到一个新的集合中返回。以下是示例代码:
List<String> filteredList = currentYearDeptNames.stream()
.distinct()
.collect(Collectors.toList());
这将返回一个过滤后的新集合,其中包含没有重复数据的元素。
对于第二部分,你可以使用filter方法来过滤掉不包含在list1中的数据。以下是示例代码:
List<JSONObject> filteredDeptList = deptList.stream()
.filter(s -> currentYearDeptNames.contains(s.getString("DEPT_NAME")))
.collect(Collectors.toList());
这将返回一个过滤后的新集合,其中包含只包含在list1中的数据的元素。
原文地址: https://www.cveoy.top/t/topic/i4fR 著作权归作者所有。请勿转载和采集!