英文:
Java lambda expression filter issue
问题
以下是翻译好的内容:
我需要从哈希映射中的列表中移除一些元素。旧方法有效。新方法不起作用,似乎没有移除请求的元素。
在旧方法中,我从未得到过预期的“找到后!”日志。而使用新方法时,我得到了一些“找到后!”的日志
旧方法:
workingMarketCards.forEach((k, v) -> {
for (FixedPriceInsertion fpi : v) {
if (fpi.getMasterId() == oc.getMasterCardId()) {
System.out.println("找到前!");
}
}
ArrayList<FixedPriceInsertion> toBeRemoved = new ArrayList<>();
for (FixedPriceInsertion fpi : v) {
if (fpi.getMasterId() == (long) (oc.getMasterCardId())) {
toBeRemoved.add(fpi);
}
}
for (FixedPriceInsertion fpi : toBeRemoved) {
v.remove(fpi);
}
for (FixedPriceInsertion fpi : v) {
if (fpi.getMasterId() == oc.getMasterCardId()) {
System.out.println("找到后!");
}
}
});
新方法:
workingMarketCards.forEach((k, v) -> {
for (FixedPriceInsertion fpi : v) {
if (fpi.getMasterId() == oc.getMasterCardId()) {
System.out.println("找到前!");
}
}
v.stream().filter(s -> s.getMasterId() == (long) (oc.getMasterCardId()));
for (FixedPriceInsertion fpi : v) {
if (fpi.getMasterId() == oc.getMasterCardId()) {
System.out.println("找到后!");
}
}
});
为什么?我的代码有什么问题?
英文:
I need to remove some elements from a list inside an hashmap. Old way works. New way won't work, seems do not remove requested elements.
Logging in Old way I never obtain "find after!", as expected. With New way I obtains some "find after!"
Old way:
workingMarketCards.forEach((k, v) -> {
for (FixedPriceInsertion fpi : v) {
if (fpi.getMasterId() == oc.getMasterCardId()) {
System.out.println("found before!");
}
}
ArrayList<FixedPriceInsertion> toBeRemoved = new ArrayList<>();
for (FixedPriceInsertion fpi : v) {
if (fpi.getMasterId() == (long) (oc.getMasterCardId())) {
toBeRemoved.add(fpi);
}
}
for (FixedPriceInsertion fpi : toBeRemoved) {
v.remove(fpi);
}
for (FixedPriceInsertion fpi : v) {
if (fpi.getMasterId() == oc.getMasterCardId()) {
System.out.println("found after!");
}
}
});
New way:
workingMarketCards.forEach((k, v) -> {
for (FixedPriceInsertion fpi : v) {
if (fpi.getMasterId() == oc.getMasterCardId()) {
System.out.println("found before!");
}
}
v.stream().filter(s -> s.getMasterId() == (long) (oc.getMasterCardId()));
for (FixedPriceInsertion fpi : v) {
if (fpi.getMasterId() == oc.getMasterCardId()) {
System.out.println("found after!");
}
}
});
Why? What wrong in my code?
答案1
得分: 3
你想要使用 removeIf()
:
v.removeIf(s -> s.getMasterId() == (long) (oc.getMasterCardId()));
v.stream().filter()
对 v
不产生任何影响;它只影响从 v
生成的元素流。
不清楚 getMasterId()
返回的是什么类型,但是谨慎使用 ==
来比较非原始类型的东西;在比较对象时,总是使用 .equals()
更为安全。
英文:
You want removeIf()
:
v.removeIf(s -> s.getMasterId() == (long) (oc.getMasterCardId()));
v.stream().filter()
does nothing to v
; it only affects the stream of elements generated from v
.
It's not clear what type getMasterId()
returns, but caution is advised using ==
for comparing things other than primitives; always use .equals()
when comparing Objects.
答案2
得分: 1
筛选器不会修改变量s
中的值。它只会将经过筛选的流传递给下一个链式方法,由于之后没有其他操作,所以它不会执行任何操作。
您需要做的是使用筛选器来删除这些值,然后收集它们。
ArrayList<FixedPriceInsertion> v2 = v.stream()
.filter(s -> s.getMasterId() != (long) (oc.getMasterCardId()))
.collect(Collectors.toList());
英文:
The filter doesn't modify the values in the variable s
. It only send the filtered stream to the next chain methods and since you don't have anything after that it won't do anything.
What you need to do is to use the filter to remove the values and the collect them.
ArrayList<FixedPriceInsertion> v2 = v.stream()
.filter(s -> s.getMasterId() != (long) (oc.getMasterCardId()))
.collect(Collectors.toList());
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论