Java lambda表达式过滤问题

huangapple go评论70阅读模式
英文:

Java lambda expression filter issue

问题

以下是翻译好的内容:

我需要从哈希映射中的列表中移除一些元素。旧方法有效。新方法不起作用,似乎没有移除请求的元素。

在旧方法中,我从未得到过预期的“找到后!”日志。而使用新方法时,我得到了一些“找到后!”的日志 Java lambda表达式过滤问题

旧方法:

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!" Java lambda表达式过滤问题

Old way:

workingMarketCards.forEach((k, v) -&gt; {
    for (FixedPriceInsertion fpi : v) {
        if (fpi.getMasterId() == oc.getMasterCardId()) {
            System.out.println(&quot;found before!&quot;);
        }
    }
    ArrayList&lt;FixedPriceInsertion&gt; toBeRemoved = new ArrayList&lt;&gt;();
    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(&quot;found after!&quot;);
        }
    }
});

New way:

workingMarketCards.forEach((k, v) -&gt; {
    for (FixedPriceInsertion fpi : v) {
        if (fpi.getMasterId() == oc.getMasterCardId()) {
            System.out.println(&quot;found before!&quot;);
        }
    }
    v.stream().filter(s -&gt; s.getMasterId() == (long) (oc.getMasterCardId()));
    for (FixedPriceInsertion fpi : v) {
        if (fpi.getMasterId() == oc.getMasterCardId()) {
            System.out.println(&quot;found after!&quot;);
        }
    }
});

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 -&gt; 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&lt;FixedPriceInsertion&gt; v2 = v.stream()
                        .filter(s -&gt; s.getMasterId() != (long) (oc.getMasterCardId()))
                        .collect(Collectors.toList());

huangapple
  • 本文由 发表于 2020年9月11日 01:20:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/63834707.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定