英文:
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);
答案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, Stream
s should be stateless. This is due to the fact that the elements in Stream
s 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论