根据其ID删除对象,并收集已删除到另一个列表的对象
我想从雇员
的列表中删除( list1
)对象,该对象在雇员
的另一个列表中不存在代码>)通过其 ID
,然后使用Java8。
示例:
List<Employee> list1 = Stream.of(
new Employee("100","Boston","Massachusetts"),
new Employee("400","Atlanta","Georgia"),
new Employee("300","pleasanton","California"),
new Employee("200","Decatur","Texas"),
new Employee("500","Cumming","Atlanta"),
new Employee("98","sula","Maine"),
new Employee("156","Duluth","Ohio"))
.collect(Collectors.toList());
从上面的列表中,需要基于 ID
对象 iD 。
List<Employee> list2 = Stream.of(
new Employee("100","Boston","Massachusetts"),
new Employee("800","pleasanton","California"),
new Employee("400","Atlanta","Georgia"),
new Employee("10","Decatur","Texas"),
new Employee("500","Cumming","Atlanta"),
new Employee("50","sula","Maine"),
new Employee("156","Duluth","Ohio"))
.collect(Collectors.toList());
预期输出: List1和List3
List<Employee> list1 = Stream.of(
new Employee("100","Boston","Massachusetts"),
new Employee("400","Atlanta","Georgia"),
new Employee("500","Cumming","Atlanta"),
new Employee("156","Duluth","Ohio"))
.collect(Collectors.toList());
List<Employee> list3 = Stream.of(
new Employee("300","pleasanton","California"),
new Employee("200","Decatur","Texas"),
new Employee("98","sula","Maine")
)
.collect(Collectors.toList());
在下面尝试过,但无法按预期工作
List<Employee> list3 = new ArrayList<>();
if(CollectionUtils.isNotEmpty(list1) && CollectionUtils.isNotEmpty(list2)){
list2.stream().forEachOrdered( l2 -> {
Optional<Employee> nonMatch = list1.stream().filter(l1 -> !l1.getId().equalsIgnoreCase(l2.getId())).findAny();
if(nonMatch.isPresent()){
list3.add(nonMatch.get());
list1.removeIf(l1 -> l1.getId().equalsIgnoreCase(nonMatch.get().getId()));
}
});
}
System.out.println(list1);
System.out.println(list3);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是两个可能的解决方案。
这是简短而简洁的,但实际上并没有从
List1
中删除元素,而是利用分区收集器来创建两个列表。将分区的收藏家视为一种双向过滤器:如果您的谓词满足,请收集到一个列表,如果不是,请收集到另一个列表。在我们的情况下,谓词实际上是“ List2是否包含一个与List1相同的ID的员工?”。为了降低实际开销,该代码从list2
forront准备了ID列表。如果您需要保留
list1
- 例如出于内存脚印的原因 - 并且确实必须从中删除元素,我会说您必须坚持使用真正的老式迭代器。这样做的原因是迭代器允许您在此过程中迭代某些集合和删除元素。下一个样本完全做到这一点。请注意,我再次准备了list2
的ID列表。Here come two possible solutions.
This one is short and concise, but does in fact not remove elements from
list1
but utilizes a partitioning collector to create the two lists. Think of the partitioning collector as kind of a two-way filter: if your predicate is fulfilled, collect to one list, if it's not, collect to the other list. The predicate in our case actually is "does list2 contain an employee with the same ID as the stream element from list1?". In order to lower the actual overhead, the code prepares a list of IDs fromlist2
up-front.If you need to keep
list1
- e.g. for memory foot-print reasons - and really have to remove the elements from it, I'd say you will have to stick to the really old-fashioned iterator. The reason for that is that iterators allow you to iterate some collection and remove elements while doing so. The next sample does exactly this. Note, that I prepared a list of IDs oflist2
up-front again.当前代码的问题:
没有任何优势。
empldeparmentx
,empleolddb
等名称list1
更难在代码上解决此问题( 时间复杂性):
ID>的
id 的 中包含的list2
(剩余:包含的时间复杂性检查是 o(1));employee
的列表中生成list1
中没有常见ID> ID
的noncommonid
中的)雇员
在list2
中包含在list1
的每个id
中, set set 在上一步获得。ID
的员工会单独导致额外的性能开销,因为每个删除都有线性时间的复杂性。一个更好的选择是使用removeAll()
。removeAll()
id
丢弃了上一步中获得的列表中存在的所有员工。总体时间复杂性 o(n + m)(
n
和m
- 是list1
中的元素数字和list2
)。输出:
链接到在线演示
Issues with your current code:
forEach
, most likely something is going wrong (*have a look at the API documentation. You can invokeforEach()
method on any type of collection without creating a stream, but keep in mind that using a multiline lambda insideforEach()
doesn't bring you any advantage over a plainfor
loop.emplDeparmentX
,empleOldDB
, etc. Nameslist1
makes more difficult to reason about the code.You can address this problem in the following steps (time complexity of each step is linear):
Set
ofid
contained in thelist2
(remainder: the time complexity of the contains check is O(1));Employee
(denoted asnonCommonId
in the code) from thelist1
that have no commonid
withEmployee
contained in thelist2
by checking everyid
from thelist1
against theSet
obtained at the previous step.id
from a list separately causes additional performance overhead because each removal has a linear time complexity. A better option will be to useremoveAll()
.removeAll()
on the listid
discard all the employee that are present in the list obtained at the previous step.The overall time complexity O(n + m) (
n
andm
- are numbers of elements in thelist1
andlist2
).Output:
A link to Online Demo
首先从List2获取不同的ID列表2
删除具有从list1的通用ID的员工并获取List3。
First get the distinct id list from list2
Remove the employees having common id from list1 and get list3.