Java 8:将流中的值赋给变量?

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

Java 8: assigning a value from a stream to a variable?

问题

I just started to learn streams in java8. I am trying to convert a basic for each loop into a stream that does the exact same thing. I have checked around here and found this:
https://stackoverflow.com/questions/56274366/convert-for-loop-into-java-stream. The issue with this is that the example collects the results at the end and I don't think I need to collect anything but to assign the value which is not explained.

the loop which I want to convert is this. As you can see I want to assign the value to the vanTire.

VanTire vanTire = null;

for (Object tire : tireList) {
    if (tire instanceof VanTire) {
        vanTire = (VanTire) tire;
    }
}

What I have so far with streams is this. How do I assign the value to the vanTire variable since filtering and casting is already done?

tireList.stream()
    .filter(VanTire.class::isInstance)
    .map(VanTire.class::cast)
英文:

I just started to learn streams in java8. I am trying to convert a basic for each loop into a stream that does the exact same thing. I have checked around here and found this:
https://stackoverflow.com/questions/56274366/convert-for-loop-into-java-stream. The issue with this is that the example collects the results at the end and I don't think I need to collect anything but to assign the value which is not explained.

the loop which I want to convert is this. As you can see I want to assign the value to the vanTire.

VanTire vanTire = null;

        for (Object tire : tireList) {
            if (tire instanceof VanTire) {
                vanTire = (VanTire) tire;
            }
        }

What I have so far with streams is this. How do I assign the value to the vanTire variable since filtering and casting is already done?

tireList.stream()
    .filter(VanTire.class::isInstance)
    .map(VanTire.class::cast)

答案1

得分: 2

使用正确的项后,您需要使用.findFirst.findAny来获取一个元素,以及orElse来提供一个默认值。

VanTire vanTire = (VanTire) tireList.stream().filter(VanTire.class::isInstance)
                                      .findAny().orElse(null);

Optional文档中有.elseGet.elseThrow

Stream文档中有.findFirst.findAny

英文:

After getting the correct items, you need to use .findFirst or .findAny to get an element, along with orElse to provide a default value

VanTire vanTire = (VanTire) tireList.stream().filter(VanTire.class::isInstance)
                                    .findAny().orElse(null);
  • .elseGet and .elseThrow in Optional documentation

  • .findFirst or .findAny inStream documentation

答案2

得分: 2

通常情况下,Stream应该是无状态的。这是因为Stream中的元素可能会无序处理。因此,我建议采用一种不在Stream内部分配值的解决方案,而是从流中产生一个可分配给所需类型的结果:

VanTire vanTire = tireList.stream()
    .filter(VanTire.class::isInstance)
    .findAny()
    .map(VanTire.class::cast)
    .orElse(null);

我使用了.orElse(null),因为这与提供的示例代码在tireList为空时的行为语义相匹配。但根据业务情况,使用Optional继续计算或调用orElseThrow(...)可能是更好的选择。

英文:

In general, Streams should be stateless. This is due to the fact that the elements in Streams might be processed out of order. Thus, I would suggest a solution that does not assing the value from within the Stream, but rather yield a result from the stream that is assignable to the desired type:

VanTire vanTire = tireList.stream()
    .filter(VanTire.class::isInstance)
    .findAny()
    .map(VanTire.class::cast)
    .orElse(null);

I used .orElse(null) since this maps to the semantics of the sample code provided wrt. the behaviour when tireList is empty. Depending on the business case, however, continuing computation with an Optional or calling orElseThrow(...) might be a better option.

huangapple
  • 本文由 发表于 2020年8月14日 05:05:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/63403188.html
匿名

发表评论

匿名网友

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

确定