我有两个ArrayList,其中包含一个包含大量数据的用户定义类的列表。列表之一包含另一个列表的元素。
我想从新的list3的list2中已经存在的list1中筛选出数据。
我可以借助循环来做到这一点,但是由于数据量很大,所以我不想使用循环。
Example:
List<Presentation> presentedDepartmentList //list 1
List<PresentationDetails> excludingDepartmentList //list 2
Both Presentation and PresentationDetails have a common field as
"departmentNumber". I want to filter out the entries in the
"excludingDepartmentList" from the "presentedDepartmentList".
由于我不熟悉Java8,因此在执行任务时遇到了困难。
谁能帮我吗?
如果根据大小为M的列表过滤大小为N的一个集合,则所需时间为O(N * M)。如果M大,首先将其转换为哈希集,那么您的时间将为O(N + M)。
Set<String> exclusions = excludingDepartmentList.stream()
.map(PresentationDetails::getDepartmentNumber)
.collect(Collectors.toSet());
List<Presentation> filtered = presentedDepartmentList.stream()
.filter(p -> !exclusions.contains(p.getDepartmentNumber()))
.collect(Collectors.toList());
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句